While...End While Statement (Visual Basic)
Runs a series of statements as long as a given condition is
While condition [ statements ] [ Continue While ] [ statements ] [ Exit While ] [ statements ] End While
||Optional. One or more statements following
||Optional. Transfers control to the next iteration of the
||Optional. Transfers control out of the
||Required. Terminates the definition of the
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. If you want to repeat the statements a set number of times, the For...Next Statement is usually a better choice.
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.
While statement always checks the condition before it starts the loop. Looping continues while the condition remains
False when you first enter the loop, it doesn’t run even once.
condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean Data Type value (
False). This expression can include a value of another data type, such as a numeric type, that has been converted to
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.
The Exit While statement can provide another way to exit a
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
When used within nested
Exit While transfers control out of the innermost loop and into the next higher level of nesting.
Continue While statement immediately transfers control to the next iteration of the loop. For more information, see Continue Statement.
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 Debug.WriteLine("") ' 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 Debug.WriteLine("") ' 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) Else Dim sr As System.IO.StreamReader = System.IO.File.OpenText(textFilePath) While sr.Peek() >= 0 Debug.WriteLine(sr.ReadLine()) End While sr.Close() End If End Sub
Send feedback about: