0

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?

2
  • Don't you need to reset y=1.d0 after you finish your x-do loop? Commented Jun 25, 2013 at 13:24
  • Put y=1.d0 between the two do whiles. Commented Jun 27, 2013 at 18:10

1 Answer 1

1

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  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.