Übersetzung vorschlagen
 
Andere Vorschläge:

progress indicator
Keine anderen Vorschläge
Per Mausklick bewerten und Feedback geben
MSDN
MSDN Library
Visual Studio 2010
Visual Studio
Visual Basic
Anweisungen A – E
 Do...Loop-Anweisung
Alle reduzieren/Alle erweitern Alle reduzieren
Inhalt anzeigen:  Englisch mit deutscher ÜbersetzungInhalt anzeigen: Englisch mit deutscher Übersetzung
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

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.

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).

NoteNote

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 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.

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.

Visual Basic
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.

Visual Basic
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.

Visual Basic
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.

Visual Basic
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

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

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.

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).

HinweisHinweis

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 Do

Die 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.

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.

Visual Basic
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.

Visual Basic
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.

Visual Basic
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.

Visual Basic
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

Datum

Versionsgeschichte

Grund

Januar 2011

Hinweise neu organisiert und Beispiele hinzugefügt.

Informationsergänzung.

Communityinhalt   Was ist Community Content?
Neuen Inhalt hinzufügen RSS  Anmerkungen
Processing
© 2012 Microsoft. Alle Rechte vorbehalten. Nutzungsbedingungen | Markenzeichen | Informationen zur Datensicherheit
Page view tracker