I have a fairly simple structure:
do i = 1,x
if (condition) then
do_stuff
exit
end if
end do
If the do
-loop gets big enough, the exit
statement wouldn't be good anymore (I look at it approx. like I look at go to
statements).
I'm looking for a cleaner way to exit the loop when appropriate so that it is also more clear what is happening for somebody else looking over my code.
One thing I thought of that might clean up would be something along the lines of
lkeepgoing = .true.
iterator = 1
while (lkeepgoing) then
if (condition) then
do_stuff
lkeepgoing = .false.
end if
iterator = iterator+1
end while
While I think it clears up why I want to exit the loop without having to look through the whole loop, it looks overly complicated I think.
So the question: How do you exit do
-loops when a condition is met in a clear, clean way that doesn't use spaghetti-code-prone statements?