Do...Loop Statement

Executes a sequence of statements while a given condition is true, or until a condition becomes true.

Syntax

Preconditional form
Do { While | Until } condition
    [ statements ]
Loop
Postconditional form
Do
    [ statements ]
Loop { While | Until } condition

Parts

Do
Required — Starts the statement.
While
Required, unless Until is used — Execute the statements until condition becomes false.
Until
Required, unless While is used — Execute the statements until condition becomes true.
condition
Required — A Boolean expression
statements
Optional — One or more statements between Do and Loop. These execute while condition is true, or until condition becomes true.
Alternatively, two or three groups of statements. The first group starts with Begin, and executes the same as specified above. The subsequent groups start with When, and execute either when the Begin group can not start, or after it completes. See the When statements section for more details.
Loop
Completes the Do...Loop statement.

Details

Use the Do...Loop structure when you must execute one or more statements again and again. It is recommended when you can not be sure how many times the statements must execute. Control depends on the Boolean condition that you specify. The preconditional form is equivalent to the While statement, but Do...Loop is more flexible. If you must execute the statements a known number of times, the For...To statement can give better performance.

Usual preconditional execution

Usual postconditional execution

Exit Do

The Exit Do statement can stop a Do...Loop statement. Exit Do immediately moves control to the statement after Loop.

When statements

To find if a Do While statement completed because the condition became false, use the When %Done statement.

To find if a Do While statement was not executed because the condition was initially false, use the When %None statement.

The postconditional form does not permit the use of When %None. The statements always execute at least one time, because the condition comes last.

See Begin...When Statements for more details.

Examples

Do While
Loop
Do
Loop While

See also