Do...Loop Statements
Repeats a block of statements while a Boolean condition is True or until the condition becomes True.
Do { While | Until } condition
[ statements ]
[ Exit Do ]
[ statements ]
Loop
-or-
Do
[ statements ]
[ Exit Do ]
[ statements ]
Loop { While | Until } condition
Parts
- While
- Required unless Until is used. Keyword. Repeat the loop until condition is False.
- Until
- Required unless While is used. Keyword. Repeat the loop until condition is True.
- condition
- Optional. Boolean. Expression that evaluates to a value of True or False.
- statements
- Optional. One or more statements that are repeated while, or until, condition is True.
Remarks
The Exit Do statement transfers control immediately to the statement following the Loop statement. Any number of Exit Do statements can be placed anywhere in the Do loop. Exit Do is often used after evaluating some condition, for example with If...Then...Else.
Example
This example shows how Do...Loop statements can be used. The inner Do...Loop statement loops 10 times, sets the value of the flag to False, and exits prematurely using the Exit Do statement. The outer loop exits immediately upon checking the value of the flag.
Dim Check As Boolean = True
Dim Counter As Integer = 0
Do ' Outer loop.
Do While Counter < 20 ' Inner loop.
Counter += 1 ' Increment Counter.
If Counter = 10 Then ' If condition is True,
Check = False ' Set value of flag to False.
Exit Do ' Exit inner loop.
End If
Loop
Loop Until Check = False ' Exit outer loop immediately.
See Also
Exit Statement | For...Next Statements | Faster For...Next Loops | Do...Loop Statements (Conceptual) | While...End While Statements