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
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.
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 = 0Do
' 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 IfLoop
Loop Until
Check = False ' Exit outer loop immediately.
Exit Statement | For...Next Statements |