Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a subroutine in a do loop - doesn't seem to work 1

Status
Not open for further replies.

stevieeeh

Technical User
Mar 20, 2012
2
GB
Hi - first post here so sorry if I miss anything out.

Basically I want to call 2 subroutines several times in a row.
Rather than write them out again and again, I thought it would be easier to put them in a do loop.

Problem is, it seems to only do the do loop once and then exit.

So the do loop looks like this:

Do i=1,n
Write(*,*) i
Call Strategy(Player1s_Strategy,Player2s_Move,Player1s_Move)
Call Strategy(Player2s_Strategy,Player1s_Move,Player2s_Move)
End Do

Now the output is simply: 1
When it should be: 1
2
etc.

This seems to tell me that it is exiting the do loop prematurely. If I remove the call statements, it prints the correct expected output.
There are no problems with the program or subroutine as if I write the call statements out again and again, the program runs fine.

What is going on? And is there another way instead of writing out the call statements again and again...

Thank you all in advance for your help and I'm really sorry if I haven't been clear enough!
 
Add print i after these calls (in the loop body). See what happens with i value...
 
I guess it would be interesting to see what the subroutine does...

Other than that...I would write the value of i and n before entering the loop and after the loop:

are you sure the just the loop is being exited? or is the entire program ending in the middle of the loop?
is there a memory problem? is n somehow being set to 1 inside the subroutine and hence, it quits the loop?...this would typically be an illegal thing to do (to modify the loop parameter inside the loop) and I am not sure if Fortran protects against it, but could it be happening by accident?

By the way, just because you write something it does not mean is going to show up on the screen...if the buffer is not full enough to make it worthwhile for the system to bother, it will wait until it is...you wanna force a write? put a flush...this way you will get everything up until the point the program crushes.


 
Thanks for your replies.

salgerman - You were right, 'i' was being used in the subroutine and so quit the loop with no error messages.

Simply changed 'i' to something else and it worked fine! Should have spotted that earlier.

You have saved me hours of frustration, thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top