Do...Loop 陳述式 (Visual Basic)Do...Loop Statement (Visual Basic)

Boolean 條件 True 或直到條件變成 True時,重複語句區塊。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

詞彙Term 定義Definition
Do 必要。Required. 啟動 Do 迴圈的定義。Starts the definition of the Do loop.
While 除非使用 Until,否則為必要參數。Required unless Until is used. 重複執行迴圈,直到 False``condition 為止。Repeat the loop until condition is False.
Until 除非使用 While,否則為必要參數。Required unless While is used. 重複執行迴圈,直到 True``condition 為止。Repeat the loop until condition is True.
condition 選擇性。Optional. Boolean 運算式。Boolean expression. 如果 Nothing``condition,Visual Basic 會將它視為 FalseIf condition is Nothing, Visual Basic treats it as False.
statements 選擇性。Optional. 一或多個在 condition True時重複的語句。One or more statements that are repeated while, or until, condition is True.
Continue Do 選擇性。Optional. 將控制權轉移至 Do 迴圈的下一個反復專案。Transfers control to the next iteration of the Do loop.
Exit Do 選擇性。Optional. 將控制權轉移 Do 迴圈。Transfers control out of the Do loop.
Loop 必要。Required. 結束 Do 迴圈的定義。Terminates the definition of the Do loop.

備註Remarks

當您想要重複一組不限次數的語句,直到滿足條件為止,請使用 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.

您可以使用 WhileUntil 來指定 condition,而不是兩者。You can use either While or Until to specify condition, but not both.

您只能在迴圈的開頭或結尾測試一次 conditionYou can test condition only one time, at either the start or the end of the loop. 如果您在迴圈開始時測試 condition (在 Do 語句中),迴圈可能不會執行一次。If you test condition at the start of the loop (in the Do statement), the loop might not run even one time. 如果您在迴圈結尾(在 Loop 語句中)進行測試,迴圈一律至少會執行一次。If you test at the end of the loop (in the Loop statement), the loop always runs at least one time.

此條件的結果通常是兩個值的比較,但它可以是評估為布林資料類型值(TrueFalse)的任何運算式。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). 這包括已轉換成 Boolean之其他資料類型(例如數數值型別)的值。This includes values of other data types, such as numeric types, that have been converted to Boolean.

您可以藉由將一個迴圈放在另一個迴圈中,來將 Do 迴圈。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.

注意

Do...Loop 結構提供比 While 更大的彈性 ... End While 語句,因為它可讓您決定在 condition 停止 True 或第一次變成 True時,是否要結束迴圈。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. 它也可讓您測試迴圈開始或結束時的 conditionIt also enables you to test condition at either the start or the end of the loop.

Exit DoExit Do

Exit Do語句可以提供結束 Do…Loop的替代方式。The Exit Do statement can provide an alternative way to exit a Do…Loop. Exit Do 會立即將控制權轉移到 Loop 語句後面的語句。Exit Do transfers control immediately to the statement that follows the Loop statement.

在評估某些條件(例如在 If...Then...Else 結構中)時,通常會使用 Exit DoExit 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. Exit Do 的其中一種用途是測試可能導致無止盡迴圈的條件,這是一種迴圈,可能會執行很長或無限次的次數。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. 您可以使用 Exit Do 來 escape 迴圈。You can use Exit Do to escape the loop.

您可以在 Do…Loop中的任何位置包含任意數目的 Exit Do 語句。You can include any number of Exit Do statements anywhere in a Do…Loop.

在嵌套 Do 迴圈中使用時,Exit Do 會將控制權從最內層的迴圈轉移到下一個較高層級的嵌套。When used within nested Do loops, Exit Do transfers control out of the innermost loop and into the next higher level of nesting.

範例Example

在下列範例中,迴圈中的語句會繼續執行,直到 index 變數大於10為止。In the following example, the statements in the loop continue to run until the index variable is greater than 10. Until 子句位於迴圈的結尾。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

下列範例會使用 While 子句,而不是 Until 子句,而且 condition 會在迴圈開始時進行測試,而不是在結尾處進行測試。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

在下列範例中,當 index 變數大於100時,condition 停止迴圈。In the following example, condition stops the loop when the index variable is greater than 100. 不過,迴圈中的 If 語句會導致 Exit Do 語句在索引變數大於10時停止迴圈。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

下列範例會讀取文字檔中的所有行。The following example reads all lines in a text file. OpenText 方法會開啟檔案,並傳回讀取字元的 StreamReaderThe OpenText method opens the file and returns a StreamReader that reads the characters. Do...Loop 條件中,StreamReaderPeek 方法會決定是否有任何額外的字元。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