brentoboy - 2005-03-31

I know of no language that allows you to break out of a loop from an inner loop.  And I have wanted to do exactly that several times.

Consider the following (the leading underscores ( _ ) are there so that the post wont kill my indentation, you can ignore them)

Loop To 10
_ Loop To 300
_ _ If SomethingBadHappened()
_ _ _ Exit Loop
_ _ End If
_ _ ... do other stuff ...
_ End Loop
End Loop

---
if something bad happens, usually, you want to break out of both loops.  Traditionally, to exit both loops, you would have to do a second "If SomethingBadHappened()" to exit the second loop - if that was your intent. or - even worse - create a magical variable that helps you decide to exit the outer loop after exiting the inner loop.

this sort of thing is error prone, and hard to maintain - suppose some idiot removes the line where you set the magical variable because he doesnt see where it affects anything. Then, you are screwed.

it would be better if you could say what you mean:

Exit Loop (2)

Meaning - exit 2 loop structures
Exit Loop (1) would be implied from a simple Exit Loop statement.  And of course, you could exit as many as you want - and the compiler could tell you when you were trying to exit a loop that is too far out.

Of course the same holds true for the other looping structures - While, Loop, For, ForEach, whatever.