Do…Loop
Statement
Runs a sequence of statements while a given condition is true, or until a condition becomes true.
Syntax
Pre-condition construct
Do { While | Until } condition
[ statements ]
Loop
Post-condition construct
Do
[ statements ]
Loop { While | Until } condition
Parts
Do
- Mandatory start of the statement.
While
- Mandatory unless you use
Until
. The loop runsstatements
untilcondition
becomes false. Until
- Mandatory unless you use
While
. The loop runsstatements
untilcondition
becomes true. condition
- Mandatory Boolean expression.
statements
- Optional one or more statements between
Do
andLoop
. These run whilecondition
is true, or untilcondition
becomes true. - The alternative is two or more groups of statements.
The first group starts with
Exit When
, and runs the same as specified above. The subsequent groups start withWhen
, and run when (1) the statements afterBegin
cannot start, or (2) after they complete. See the When statements section for more information. Loop
- Completes the statement.
Instructions
Use Do…Loop
when you must run one or more statements again and again.
It is recommended when you cannot be sure how many times the statements must run.
Control is connected with the Boolean condition
that you supply.
The pre-condition construct is equivalent to the statement While
, but Do…Loop
is more flexible.
If you must run the statements a known number of times, the statement For
can give better performance.
Usual pre-conditional operation
TODO
Usual post-conditional operation
TODO
Exit Do
The statement Exit Do
can stop the operation of Do…Loop
.
Exit Do
immediately moves control to the statement after Loop
.
When statements
To find if a loop with Do While
completed because condition
became false, use the statement When DONE
.
To find if a loop with Do While
was not run because condition
was initially false, use the statement When NONE
.
The post-condition construct does not let you use When NONE
.
The statements always run a minimum of one time, because the condition comes last.
The Post-condition construct permits you to write the initial line as Do Exit When
.
This is the only statement permitted on the same line with Do
.
See Exit When Clause for more information.
Examples
Pre-condition example
Var factorial = 1
Var counter = 5
Do While counter > 0
factorial *= counter
counter -= 1
Loop
PrintLine factorial
Post-condition example
Var factorial = 1
Var counter = 5
Do
factorial *= counter
counter -= 1
Loop While counter > 0
PrintLine factorial