Do...Loop Statement

Repeats a block of statements while a condition is True or until a condition becomes True.

                      Do [{While | Until} condition]
   [statements]
   [Exit Do]
   [statements]
Loop               ' or use this syntax
Do
   [statements]
   [Exit Do]
   [statements]
Loop [{While | Until} condition]

Arguments

  • Do
    Required. Starts the definition of the Do loop.

  • While
    Required, unless Until is used. Specifies that the loop should be repeated until condition is False.

  • Until
    Required, unless While is used. Specifies that the loop should be repeated until condition is True.

  • condition
    Optional. Numeric or string expression that is True or False. If condition is Null, condition is treated as False.

  • statements
    Optional. One or more statements that are repeated while or until condition is True.

  • Exit Do
    Optional. Transfers control out of the Do loop.

  • Loop
    Required. Ends the definition of the Do loop.

Remarks

Use a Do...Loop structure when you want to repeat a set of statements an indefinite number of times, until a condition is satisfied. If you want to repeat the statements a set number of times, the For...Next Statement is usually a better choice.

You can use either While or Until to specify the condition, but not both.

You can test the condition only one time, at either the start or the end of the loop. If you test the condition at the start of the loop (in the Do statement), the loop might never run. If you test the condition at the end of the loop (in the Loop statement), the loop always runs at least one time.

You can nest Do loops by including one loop within another. You can also nest different kinds of control structures within each other.

Exit Do

The Exit Do statement can provide an alternative way to exit a Do...Loop. Often used with the evaluation of some condition (for example, If...Then), Exit Do transfers control to the statement immediately following the loop. Any number of Exit Do statements can be included anywhere in the Do...Loop.

When used within nested Do...Loop statements, Exit Do transfers control to the loop that is nested one level above the loop in which it occurs.

One use of Exit Do is to test for a condition that could cause an endless loop, which is a loop that could run a very large or even infinite number of times. You can use Exit Do to escape the loop. Otherwise, the loop continues running.

Example

In the following example, the statements in the loop continue to execute until the index variable is greater than 10. The Until clause is at the end of the loop.

Dim index
index = 0
Do
    document.write(index & " ")
    index = index + 1
Loop Until index > 10
' Output: 0 1 2 3 4 5 6 7 8 9 10

The following example uses a While clause instead of an Until clause, and the condition is tested at the start of the loop instead of at the end.

index = 0
Do While index <= 10
    document.write(index & " ")
    index = index + 1
Loop
' Output: 0 1 2 3 4 5 6 7 8 9 10

In the following example, the condition would terminate the loop when the index variable is greater than 100. The If statement in the loop, however, causes the Exit Do statement to terminate the loop when the index variable is greater than 10.

index = 0
Do While index <= 100
    If index > 10 Then
        Exit Do
    End If

    document.write(index & " ")
    index = index + 1
Loop
' Output: 0 1 2 3 4 5 6 7 8 9 10

Requirements

Version 1

See Also

Reference

Exit Statement

For...Next Statement

While...Wend Statement

Change History

Date

History

Reason

August 2009

Added to arguments table and remarks, and added examples.

Information enhancement.