Visual Studio 2010 - Visual Basic Do...Loop Statement (Visual Basic) 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 ]
Loop
-or-
Do
[ statements ]
[ Exit Do ]
[ statements ]
Loop { While | Until } condition

Parts
Term | Definition | Do | Required. Starts the definition of the Do loop. | While | Required unless Until is used. Repeat the loop until condition is False. | Until | Required unless 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. | 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. 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 (Visual Basic) 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 (Visual Basic). Note |
|---|
The Do...Loop structure gives you more flexibility than the While...End While Statement (Visual Basic) 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 DoThe 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
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
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
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
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

Change History
Date | History | Reason |
January 2011
| Reorganized the remarks and added examples. |
Information enhancement.
|
|
Visual Studio 2010 - Visual Basic Do...Loop-Anweisung (Visual Basic) Wiederholt einen Block mit Anweisungen, solange eine Boolean-Bedingung True ist bzw. bis die Bedingung True wird.
Do { While | Until } condition
[ statements ]
[ Exit Do ]
[ statements ]
Loop
-or-
Do
[ statements ]
[ Exit Do ]
[ statements ]
Loop { While | Until } condition

Bestandteile
Ausdruck | Definition | Do | Erforderlich. Leitet die Definition der Do-Schleife ein. | While | Erforderlich, sofern nicht Until verwendet wird. Die Schleife wird wiederholt, bis condition False ist. | Until | Erforderlich, sofern nicht While verwendet wird. Die Schleife wird wiederholt, bis condition True ist. | condition | Optional. Boolean-Ausdruck. Wenn condition Nothing ist, behandelt Visual Basic den Ausdruck als False. | statements | Optional. Eine oder mehrere Anweisungen, die wiederholt werden, solange condition True ist bzw. bis die Bedingung True wird. | Exit Do | Optional. Überträgt die Steuerung aus der Do-Schleife. | Loop | Erforderlich. Beendet die Definition der Do-Schleife. |

Hinweise
Verwenden Sie eine Do...Loop-Struktur, wenn eine Gruppe von Anweisungen wiederholt werden soll, bis eine Bedingung zutrifft. Wenn die Anweisungen mit einer festgelegten Anzahl von Wiederholungen ausgeführt werden sollen, ist die For...Next Statement i. d. R. vorzuziehen. Sie können condition entweder mit While oder mit Until angeben, jedoch nicht mit beiden Bedingungen. Sie können condition nur einmal, am Anfang oder Ende der Schleife, testen. Wenn Sie condition am Anfang der Schleife testen (in der Do-Anweisung), wird die Schleife möglicherweise niemals ausgeführt. Wenn Sie die Bedingung am Ende der Schleife testen (in der Loop-Anweisung), wird die Schleife immer mindestens einmal ausgeführt. Die Bedingung ergibt sich normalerweise durch einen Vergleich zweier Werte. Es kann sich jedoch um einen beliebigen Ausdruck handeln, der zu einem Boolean-Datentyp (Visual Basic)-Wert (True oder False) ausgewertet wird. Dazu gehören Werte anderer Datentypen, wie z. B. numerische Typen, die in Boolean umgewandelt wurden. Sie können Do-Schleifen schachteln, indem Sie eine Schleife in eine andere einfügen. Sie können auch unterschiedliche Arten von Steuerungsstrukturen ineinander schachteln. Weitere Informationen finden Sie unter Geschachtelte Steuerungsstrukturen (Visual Basic). Hinweis |
|---|
Die Do...Loop-Struktur ermöglicht mehr Flexibilität als die While...End While-Anweisung (Visual Basic), weil Sie festlegen können, ob die Schleife beendet werden soll, wenn condition nicht mehr True ist oder wenn die Bedingung das erste Mal True ist. Außerdem können Sie condition am Anfang oder am Ende der Schleife testen. |
Exit DoDie Exit Do-Anweisung ist eine alternative Möglichkeit zum Beenden eines Do…Loop. Exit Do überträgt die Steuerung direkt an die erste Anweisung nach der Loop-Anweisung. Exit Do wird oft nach der Auswertung einer Bedingung verwendet, z. B. in einer If...Then...Else-Struktur. Möglicherweise möchten Sie eine Schleife beenden, wenn Sie eine Bedingung feststellen, die das Fortsetzen des Durchlaufs unnötig oder unmöglich macht, z. B. ein fehlerhafter Wert oder eine Anforderung zum Beenden. Die Verwendung von Exit Do ist sinnvoll, um eine Bedingung zu testen, die eine Endlosschleife verursachen kann. Hierbei handelt es sich um eine Schleife, die mit einer sehr großen oder unendlichen Anzahl von Wiederholungen ausgeführt werden kann. Sie können die Schleife mit Exit Do verlassen. Sie können eine beliebige Anzahl von Exit Do-Anweisungen überall in einer Do…Loop einschließen. Bei Verwendung in geschachtelten Do-Schleifen überträgt Exit Do die Steuerung aus der innersten Schleife auf die nächsthöhere Schachtelungsebene.

Beispiel
Im folgenden Beispiel werden weiterhin die Anweisungen in der Schleife ausgeführt, bis die index-Variable größer als 10 ist. Die Until-Klausel ist am Ende der Schleife.
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
Im folgenden Beispiel wird eine While-Klausel anstelle von einer Until-Klausel verwendet, und condition wird am Anfang der Schleife statt am Ende getestet.
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
Im folgenden Beispiel beendet condition die Schleife, wenn die index-Variable größer als 100 ist. Die If-Anweisung in der Schleife führt jedoch dazu, dass die Exit Do-Anweisung beendet wird, um die Schleife zu stoppen, wenn die Index-Variable größer als 10 ist.
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
Im folgende Beispiel werden alle Zeilen in einer Textdatei gelesen. Die OpenText-Methode öffnet die Datei und gibt einen StreamReader zurück, der die Zeichen liest. In der Do...Loop-Bedingung bestimmt die Peek-Methode des StreamReader, ob zusätzlichen Zeichen vorhanden sind.
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

Siehe auch

Änderungsprotokoll
Datum | Versionsgeschichte | Grund |
Januar 2011
| Hinweise neu organisiert und Beispiele hinzugefügt. |
Informationsergänzung.
|
|