28
This construct causes the sequence of zero or more statements to be repeatedly executed. However,
execution of the loop may be terminated using an Exit Do statement at which point control will transfer
to the first statement following the Loop statement. Note that the Do-Loop compound statement may be
nested and the Exit Do only terminates the innermost Do-Loop that contains it.
Example
Do
<other-statements>
Do
<other-statements>
If (i > 5) Then
<other-statements>
Exit Do
End If
Loop
<other-statements>
Loop
Here, the Exit Do only terminates the inner Do-Loop; the outer one continues to iterate.
There are four other variations on this basic looping concept, all of which involve a condition for
continuing the iteration. The syntax of the four variations is as follows:
Do While <boolean-expression>
[<statements>]
Loop
Do Until <boolean-expression>
[<statements>]
Loop
Do
[<statements>]
Loop While <boolean-expression>
Do
[<statements>]
Loop Until <boolean-expression>
As you can see, the difference between the four variations is whether the test is at the top of the loop or
at the bottom of the loop and, secondly, the logic sense of the condition. Testing the condition at the top
of the loop means that the statements within the loop may be executed zero or more times. Testing the
condition at the bottom of the loop means that the statements will always be executed at least once.
The difference between using While and Until is nothing more than a logic inversion. The construct Do
While Not <boolean-expression> is logically equivalent to Do Until <boolean-expression>.
There is no fixed limit on how deeply Do loops may be nested. The actual limit is governed by how much
memory is available to the compiler. For all practical purposes, there is no limit.
BasicX Compatibility Note
In BasicX mode, the nesting of Do loops is limited to 10 for compatibility.