Do...Loop Statements

These instructions bound a block of instructions that are repeatedly executed so long as a specified expression evaluates to True or until the expression value becomes True.

Do While condition
    
[statements]
Loop

-or-

Do Until condition
    
[statements]
Loop

-or-

Do
     [statements]
Loop While condition

-or-

Do
     [statements]
Loop Until condition

Prerequisites

None

Parameters

condition

Required expression that is interpreted as a True or False value. Any expression that yields a numeric result can be specified, not just Boolean expressions. Any expression that evaluates to <>0 is interpreted as a True condition.

statements

Optional statement or list of statements that are repeatedly executed within the control structure.

Remarks

This control structure either tests a condition at the start or the end of a block of statements and repeatedly executes the statements so long as the condition is True or until it becomes True. It can be used to implement program instruction loops.

For the Do While and Do Until forms of this control structure, the condition test is performed prior to the execution of the statements. If the condition permits the loop to be executed, the statements will be executed once. At the conclusion of the loop, the test is repeated to determine if the statements should be executed again. So long as the condition permits execution, the statements will be repeatedly executed. If not, execution of the statements is terminated. In any case, if the condition does not permit the execution of the loop on the first test, the statements are never executed.

In contrast, for the Loop While or Loop Until forms of this control structure, the statements will always be executed at least one time. For these forms, the test is performed at the conclusion of the execution of the statements. So long as the condition permits execution, the statements will be repeated executed. However, if the condition does not permit the execution of the loop on the first test, the statements will still have been executed one time.

For all forms of this control structure, when the condition test is not satisfied, program execution continues at the first statement following the Loop instruction.

If the While form of the condition test is specified, the condition is satisfied and execution of the statements is permitted so long as the value of the condition is True. For the Until form of the condition test, the condition is satisfied and execution is permitted until the condition becomes True.

For more complex logic, multiple Do… Loop sequences can be nested to an arbitrary depth and can be combined with other nested control structures. For example, a Do… Loop can contain an If…Then…End If sequence which can in turn contain a While…End While sequence.

Execution of the Do loop can be terminated by a number of different methods: the condition can be set to a value that does not satisfied the test; execution can be explicitly transferred to an instruction outside of the loop, e.g. by the execution of a GoTo instruction; or an Exit Do instruction can be executed.

When an Exit Do statement is encountered, execution of the innermost Do…Loop sequence is immediately terminated and execution continues at the instruction following the Loop statement. There can be none or several Exit Do statements within each Do loop.

Examples

Dim count As Integer
count = 10
Do ' Embedded statements always execute at least once
If count = 5 Then
Exit Do ' Prematurely stops Do loop
End If
count -= 1 ' Same as “count = count-1”
Loop Until count <= 0

See Also

Statements |For…Next Statements | GoTo Statements | If…Then…Else…End If Statements | While…End While Statements