Вопрос

I have the following part in my code.

 x=1.d0
 y=1.d0
 do while (x<5.0)
   do while (y<3.0)
      print*,'x=',x,'  y=',y
      y=y+1.d0
   end do
   print*,'x================',x
   x=x+1.d0
 end do  

Here my intention is to vary y-value through y-do-loop for each update in x of the x-do-loop. However, after the first x-loop, y-loop does not execute anymore, i.e. I get the following output.

x=   1.0000000000000000        y=   1.0000000000000000     
 x=   1.0000000000000000        y=   2.0000000000000000     
 x================   1.0000000000000000     
 x================   2.0000000000000000     
 x================   3.0000000000000000     
 x================   4.0000000000000000

Can't I use do while for my purpose? If yes, what modification can be done?

Это было полезно?

Решение

You are not resetting the value of y in your outer loop, so after y increments to 3, it is never again less than 3. You should try:

x=1.d0
do while (x<5.0)
   y=1.d0
   do while (y<3.0)
      print*,'x=',x,'  y=',y
      y=y+1.d0
   end do
   print*,'x================',x
   x=x+1.d0
end do  
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top