This documentation is archived and is not being maintained.

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 ]


   [ statements ]
[ Exit Do ]
   [ statements ]
Loop { While | Until } condition


Required unless Until is used. Keyword. Repeat the loop until condition is False.
Required unless While is used. Keyword. Repeat the loop until condition is True.
Optional. Boolean. Expression that evaluates to a value of True or False.
Optional. One or more statements that are repeated while, or until, condition is True.


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 = 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 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