What repeats a statement or group of statements while a given condition is true?

Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Do...Loop Statement (Visual Basic)

  • Article
  • 09/29/2022
  • 4 minutes to read

In this article

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

Syntax

Do [ { While | Until } condition ]
    [ statements ]
    [ Continue Do ]
    [ statements ]
    [ Exit Do ]
    [ statements ]
Loop
' -or-
Do
    [ statements ]
    [ Continue Do ]
    [ statements ]
    [ Exit Do ]
    [ statements ]
Loop [ { While | Until } condition ]

Parts

TermDefinition
Do Required. Starts the definition of the Do loop.
While Cannot be given if Until is used. Repeat the loop until condition is False.
Until Cannot be given if While is used. Repeat the loop until condition is True.
condition Optional. Boolean expression. If condition is Nothing, Visual Basic treats it as False.
statements Optional. One or more statements that are repeated while, or until, condition is True.
Continue Do Optional. Transfers control to the next iteration of the Do loop.
Exit Do Optional. Transfers control out of the Do loop.
Loop Required. Terminates 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 condition, but not both. If you give neither, the loop continues until an Exit transfer control out of the loop.

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

The condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean Data Type value (True or False). This includes values of other data types, such as numeric types, that have been converted to Boolean.

You can nest Do loops by putting one loop within another. You can also nest different kinds of control structures within each other. For more information, see Nested Control Structures.

Note

The Do...Loop structure gives you more flexibility than the While...End While Statement because it enables you to decide whether to end the loop when condition stops being True or when it first becomes True. It also enables you to test condition at either the start or the end of the loop.

Exit Do

The Exit Do statement can provide an alternative way to exit a Do…Loop. Exit Do transfers control immediately to the statement that follows the Loop statement.

Exit Do is often used after some condition is evaluated, for example in an If...Then...Else structure. You might want to exit a loop if you detect a condition that makes it unnecessary or impossible to continue iterating, such as an erroneous value or a termination request. 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 large or even infinite number of times. You can use Exit Do to escape the loop.

You can include any number of Exit Do statements anywhere in a Do…Loop.

When used within nested Do loops, Exit Do transfers control out of the innermost loop and into the next higher level of nesting.

Example 1

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

Dim index As Integer = 0
Do
    Debug.Write(index.ToString & " ")
    index += 1
Loop Until index > 10

Debug.WriteLine("")
' Output: 0 1 2 3 4 5 6 7 8 9 10 

Example 2

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

Dim index As Integer = 0
Do While index <= 10
    Debug.Write(index.ToString & " ")
    index += 1
Loop

Debug.WriteLine("")
' Output: 0 1 2 3 4 5 6 7 8 9 10 

Example 3

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

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

    Debug.Write(index.ToString & " ")
    index += 1
Loop

Debug.WriteLine("")
' Output: 0 1 2 3 4 5 6 7 8 9 10 

Example 4

The following example reads all lines in a text file. The OpenText method opens the file and returns a StreamReader that reads the characters. In the Do...Loop condition, the Peek method of the StreamReader determines whether there are any additional characters.

Private Sub ShowText(ByVal textFilePath As String)
    If System.IO.File.Exists(textFilePath) = False Then
        Debug.WriteLine("File Not Found: " & textFilePath)
    Else
        Dim sr As System.IO.StreamReader = System.IO.File.OpenText(textFilePath)

        Do While sr.Peek() >= 0
            Debug.WriteLine(sr.ReadLine())
        Loop

        sr.Close()
    End If
End Sub

See also

  • Loop Structures
  • For...Next Statement
  • Boolean Data Type
  • Nested Control Structures
  • Exit Statement
  • While...End While Statement

Feedback

Submit and view feedback for

Which loop repeats a set of statements until the given condition is true?

The repeat / until loop is a loop that executes a block of statements repeatedly, until a given condition evaluates to true . The condition will be re-evaluated at the end of each iteration of the loop, allowing code inside the loop to affect the condition in order to terminate it.

Is a type of loop that is used to repeat the statement or group of statements while given condition?

A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.

Which loop is performed when the condition is true?

With a do while loop the condition is not evaluated until the end of the loop. Because of that a do while loop will always execute at least once. A for-loop always makes sure the condition is true before running the program.

What is the condition for a while loop to repeat?

A while loop evaluates condition inside the parenthesis () . If condition evaluates to true , the code inside the while loop is executed. condition is evaluated again. This process continues until the condition is false .