0

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?

Fl.pf.
  • 324
  • 1
  • 5
  • 18
  • 2
    If it helps, view `exit` as very different from a `goto`, if you must see it as a `goto` then a `goto` where the target has to be in a very specific and well-defined place. – francescalus Apr 26 '17 at 14:52
  • Note, that your two code snippets are not doing the same, in the first you probably want to have the do_stuff before the if condition. Then you could also have a single line for the exit: `if (condition) EXIT`. The standard even says, that it will do exactly this for a while loop: If loop-control is [ , ] WHILE (scalar-logical-expr), the effect is as if loop-control were omitted and the following statement inserted as the first statement of the do-block: `IF (.NOT. (scalar-logical-expr )) EXIT`. – haraldkl Apr 27 '17 at 05:26
  • In addition to the great advice by HighPerformanceMark, sometimes you can employ the `do while` loop. – Vladimir F Героям слава Apr 27 '17 at 06:49
  • 1
    My reasoning for the closure: 1. Eventhough the other question uses different name for `exit` (`break`), it is the same and that question is not language specific (no language tag). 2. That question got closed even back then, when the rules were more relaxed. Today's equivalent of that closure reason is "primarily opinion based". – Vladimir F Героям слава Apr 27 '17 at 06:53

1 Answers1

3

There's nothing wrong with the way your first code snippet uses exit. A little jump such as that is hardly spaghetti code. And compared with your second snippet it's considerably clearer that you are making a disciplined early exit from a loop when a certain condition is met.

Bear in mind that exit is very confined in where it can jump to, while go to can be used to jump all over the place.

Don't adhere to one-size-fits-none rules such as never use goto too strictly, certainly not when they make code worse.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
  • 1
    Also, there is the possibility to have named loops. In this case, the flow is even more clear. The loop starts as `my_super_loop: do ...`, ends as `end do my_super_loop`. Within that loop you can use `exit my_super_loop`. It is especially useful for nested loops. – Pierre de Buyl Apr 26 '17 at 17:13
  • That was my thinking also. But my superior told me it's bad practice and I can't use it... – Fl.pf. Apr 26 '17 at 17:46
  • I kind of could see his argumentation, but I can't find a way thats better.. – Fl.pf. Apr 26 '17 at 17:54
  • 2
    if you must abide by your superior's rules then shouldn't you be asking him this question? – agentp Apr 26 '17 at 18:26