While...End While Statement (Visual Basic)

Runs a series of statements as long as a given condition is True.

While condition
    [ statements ]
    [ Continue While ]
    [ statements ]
    [ Exit While ]
    [ statements ]
End While





Required. Boolean expression. If condition is Nothing, Visual Basic treats it as False.


Optional. One or more statements following While, which run every time condition is True.

Continue While

Optional. Transfers control to the next iteration of the While block.

Exit While

Optional. Transfers control out of the While block.

End While

Required. Terminates the definition of the While block.


Use a While...End While structure when you want to repeat a set of statements an indefinite number of times, as long as a condition remains True. If you want more flexibility with where you test the condition or what result you test it for, you might prefer the Do...Loop Statement (Visual Basic). If you want to repeat the statements a set number of times, the For...Next Statement (Visual Basic) is usually a better choice.

If condition is True, all of the statements run until the End While statement is encountered. Control then returns to the While statement, and condition is again checked. If condition is still True, the process is repeated. If it’s False, control passes to the statement that follows the End While statement.

The While statement always checks the condition before it starts the loop. Looping continues while the condition remains True. If condition is False when you first enter the loop, it doesn’t run even once.

The condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean Data Type (Visual Basic) value (True or False). This expression can include a value of another data type, such as a numeric type, that has been converted to Boolean.

You can nest While loops by placing one loop within another. You can also nest different kinds of control structures within one another. For more information, see Nested Control Structures (Visual Basic).

Exit While

The Exit While statement can provide another way to exit a While loop. Exit While immediately transfers control to the statement that follows the End While statement.

You typically use Exit While 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. You can use Exit While when you test for a condition that could cause an endless loop, which is a loop that could run an extremely large or even infinite number of times. You can then use Exit While to escape the loop.

You can place any number of Exit While statements anywhere in the While loop.

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

The Continue While statement immediately transfers control to the next iteration of the loop. For more information, see Continue Statement (Visual Basic).


In the following example, the statements in the loop continue to run until the index variable is greater than 10.

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

' Output: 0 1 2 3 4 5 6 7 8 9 10 

The following example illustrates the use of the Continue While and Exit While statements.

Dim index As Integer = 0
While index < 100000
    index += 1

    ' If index is between 5 and 7, continue 
    ' with the next iteration. 
    If index >= 5 And index <= 8 Then 
        Continue While 
    End If 

    ' Display the index.
    Debug.Write(index.ToString & " ")

    ' If index is 10, exit the loop. 
    If index = 10 Then 
        Exit While 
    End If 
End While

' Output: 1 2 3 4 9 10

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 While condition, the Peek method of the StreamReader determines whether the file contains additional characters.

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

        While sr.Peek() >= 0
        End While

    End If 
End Sub

See Also


Do...Loop Statement (Visual Basic)

For...Next Statement (Visual Basic)

Boolean Data Type (Visual Basic)

Exit Statement (Visual Basic)

Continue Statement (Visual Basic)


Loop Structures (Visual Basic)

Nested Control Structures (Visual Basic)