當迴圈計數器接近其最終值時,重複一組語句。
語法
For counter [ As datatype ] = start To end [ Step step ]
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ counter ]
組件
| 部分 | 說明 |
|---|---|
counter |
在 For 陳述句中必需的。 數值變數。 迴圈的控制變數。 如需詳細資訊,請參閱本主題稍後的 CounterArgument 。 |
datatype |
選擇性。
counter 的數據類型。 如需詳細資訊,請參閱本主題稍後的 CounterArgument 。 |
start |
必須的。 數值表達式。
counter 的初始值。 |
end |
必須的。 數值表達式。
counter的最終值。 |
step |
選擇性。 數值表達式。 每次透過迴圈遞增的量 counter 。 |
statements |
選擇性。 介於 For 和 Next 之間的 一個或多個語句會被執行指定的次數。 |
Continue For |
選擇性。 將控制權傳輸至下一個迴圈反覆運算。 |
Exit For |
選擇性。 將控制權移出 For 迴圈。 |
Next |
必須的。 結束 For 迴圈的定義。 |
備註
關鍵詞 To 會用於這個語句,以指定計數器的範圍。 您也可以在 Select...Case 語句 和陣列宣告中使用這個關鍵詞。 如需有關陣列宣告的詳細資訊,請參閱 Dim 語句。
簡單範例
當您想要重複一組語句一組次數時,請使用 For...Next 結構。
在下列範例中, index 變數的開頭為 1,且會隨著迴圈的每個反覆專案遞增,在值 index 達到 5 之後結束。
For index As Integer = 1 To 5
Debug.Write(index.ToString & " ")
Next
Debug.WriteLine("")
' Output: 1 2 3 4 5
在下列範例中,number 變數從 2 開始,並在迴圈的每次迭代中減少 0.25,當 number 的值達到 0 時結束。
Step 參數在 -.25 迴圈的每次迭代中將值減少 0.25。
For number As Double = 2 To 0 Step -0.25
Debug.Write(number.ToString & " ")
Next
Debug.WriteLine("")
' Output: 2 1.75 1.5 1.25 1 0.75 0.5 0.25 0
小提示
當您事先不知道在迴圈中執行語句的次數時,While...End While 語句 或 Do...Loop 語句 運作良好。 不過,當您預期執行迴圈的特定次數時, For...Next 迴圈是較佳的選擇。 當您第一次進入迴圈時,您可以決定反覆項目的數目。
巢狀迴圈
您可以透過將一個迴圈放入另一個迴圈中來實現巢狀 For 迴圈。 下列範例示範具有不同步驟值的巢狀 For...Next 結構。 外層迴圈會針對迭代的每次執行建立字串。 內部迴圈會在每次反覆執行時遞減迴圈計數器變數。
For indexA = 1 To 3
' Create a new StringBuilder, which is used
' to efficiently build strings.
Dim sb As New System.Text.StringBuilder()
' Append to the StringBuilder every third number
' from 20 to 1 descending.
For indexB = 20 To 1 Step -3
sb.Append(indexB.ToString)
sb.Append(" ")
Next indexB
' Display the line.
Debug.WriteLine(sb.ToString)
Next indexA
' Output:
' 20 17 14 11 8 5 2
' 20 17 14 11 8 5 2
' 20 17 14 11 8 5 2
巢狀循環時,每個迴圈都必須有唯 counter 一的變數。
您也可以將不同類型的控制結構嵌套在一起。 如需詳細資訊,請參閱 巢狀控件結構。
結束 For 迴圈 和 繼續 For 迴圈
Exit For語句會立即結束 For...
Next 迴圈,並將控制權傳送至跟在 Next 之後的語句。
語句會 Continue For 立即將控制權傳送至迴圈的下一次迭代。 如需詳細資訊,請參閱 continue 陳述。
下列範例說明如何使用Continue For和Exit For語句。
For index As Integer = 1 To 100000
' If index is between 5 and 7, continue
' with the next iteration.
If index >= 5 AndAlso index <= 8 Then
Continue For
End If
' Display the index.
Debug.Write(index.ToString & " ")
' If index is 10, exit the loop.
If index = 10 Then
Exit For
End If
Next
Debug.WriteLine("")
' Output: 1 2 3 4 9 10
您可以在 Exit For 中放任意數目的 For 語句。
Next 迴圈。 在巢狀結構 For... 內使用時
Next 迴圈,Exit For 結束最內部的迴圈,並將控制權轉移至下一個較高的巢狀層級。
Exit For 通常在您評估某些條件之後使用(例如,在 If...Then...Else 結構中)。 您可能要針對下列條件使用 Exit For :
繼續反覆運算是不必要的或不可能的。 錯誤值或終止要求可能會建立此條件。
A
Try...Catch...Finally語句會攔截例外狀況。 您可以在Exit For區塊的結尾使用Finally。您有一個無休止的迴圈,這是可以執行大量甚至無限次數的迴圈。 如果您偵測到這類條件,您可以使用
Exit For來逸出迴圈。 如需詳細資訊,請參閱 Do...迴圈陳述句。
技術實作
當 For...Next 循環啟動時,Visual Basic 會評估 start、end 和 step。 Visual Basic 目前只會評估這些值,然後將 指派 start 給 counter。 在語句區塊執行之前,Visual Basic 會 counter 比較 與 end。 如果 counter 已經大於 end 值(如果 step 為負數,則 counter 需小於 end), 迴圈會結束,控制將傳遞至 語句後面的語句。 否則,語句區塊會執行。
每次 Visual Basic 遇到 Next 語句時,它會遞增 counterstep ,並傳回 語句 For 。 同樣地,它會將 counter 與 end 比較,然後根據結果再次執行區塊或結束迴圈。 此過程將持續進行,直到 counter 超過 end 或遇到 Exit For 語句。
迴圈不會停止,直到 counter 傳遞 end為止。 如果 counter 等於 end,迴圈會繼續。 判斷是否執行區塊的比較是 counter<= end 如果 step 為正數,而 counter>= end 如果 step 為負數。
如果您在循環中更改counter的值,程式代碼可能變得較難閱讀和偵錯。 變更 start、end 或 step 的值不會影響在第一次進入迴圈時所確定的迭代值。
如果您進行巢狀迴圈,當編譯程式在遇到Next內部層級的語句之前遇到Next外部層級的語句時,會發出錯誤訊息。 不過,只有在您在每個counter語句中指定Next時,編譯程式才能偵測到這個重疊的錯誤。
步驟參數
的值 step 可以是正數或負值。 此參數會根據下表決定循環處理:
| 步驟值 | 迴圈在條件成立時執行 |
|---|---|
| 正數或零 |
counter
<= end |
| 陰性 |
counter
>= end |
step 的預設值為 1。
反駁論點
下表指出 counter 是否定義了一個範圍涵蓋整個 For…Next 迴圈的新局部變數。 此判斷取決於datatype是否存在,以及counter是否已定義。
datatype 存在嗎? |
counter 是否已定義? |
結果 (是否 counter 定義範圍為整個 For...Next 迴圈的新局部變數) |
|---|---|---|
| 否 | 是的 | 否,因為 counter 已經定義。 如果 counter 的範圍不屬於程序的本機範圍,就會發生編譯時期警告。 |
| 否 | 否 | 是的。 數據類型是從 start、 end和 step 表示式推斷而來。 如需類型推斷的相關信息,請參閱 Option Infer 語句 和 本機類型推斷。 |
| 是的 | 是的 | 是,但前提是現有的 counter 變數是在程式外部定義。 該變數會保持個別。 如果現有 counter 變數的範圍是程式的局部變數,就會發生編譯時期錯誤。 |
| 是的 | 否 | 是的。 |
的 counter 數據類型會決定反覆運算的類型,這必須是下列其中一種類型:
Byte、SByte、UShort、Short、UInteger、Integer、ULong、Long、Decimal、Single或Double。您使用 Enum 語句宣告的列舉。
一個
Object。具有下列運算子的類型
T,其中B是可用於表達式的類型Boolean。Public Shared Operator >= (op1 As T, op2 As T) As BPublic Shared Operator <= (op1 As T, op2 As T) As BPublic Shared Operator - (op1 As T, op2 As T) As TPublic Shared Operator + (op1 As T, op2 As T) As T
您可以選擇性地在語句中counter指定 Next 變數。 此語法可改善程式的可讀性,特別是如果您有巢狀 For 循環時。 您必須指定出現在對應 For 語句中的變數。
start、end 和 step 運算式可以計算為任何可轉換為 counter 類型的類型。 如果您使用使用者定義型別counter,您可能必須定義CType轉換運算符,以將startend的類型或step的類型轉換成counter。
範例 1
下列範例會從泛型清單中移除所有元素。 而不是 For Each...下一個語句,範例中示範以遞減順序進行的 For...Next 語句。 此範例會使用這項技術,因為 removeAt 方法會在移除的項目之後使元素具有較低的索引值。
Dim lst As New List(Of Integer) From {10, 20, 30, 40}
For index As Integer = lst.Count - 1 To 0 Step -1
lst.RemoveAt(index)
Next
Debug.WriteLine(lst.Count.ToString)
' Output: 0
範例 2
下列範例會逐一反覆運算使用 Enum 語句宣告的列舉。
Public Enum Mammals
Buffalo
Gazelle
Mongoose
Rhinoceros
Whale
End Enum
Public Sub ListSomeMammals()
For mammal As Mammals = Mammals.Gazelle To Mammals.Rhinoceros
Debug.Write(mammal.ToString & " ")
Next
Debug.WriteLine("")
' Output: Gazelle Mongoose Rhinoceros
End Sub
範例 3
在下列範例中,語句參數會使用一個具有+、-、>=和<=運算符多載的類別。
Private Class Distance
Public Property Number() As Double
Public Sub New(ByVal number As Double)
Me.Number = number
End Sub
' Define operator overloads to support For...Next statements.
Public Shared Operator +(ByVal op1 As Distance, ByVal op2 As Distance) As Distance
Return New Distance(op1.Number + op2.Number)
End Operator
Public Shared Operator -(ByVal op1 As Distance, ByVal op2 As Distance) As Distance
Return New Distance(op1.Number - op2.Number)
End Operator
Public Shared Operator >=(ByVal op1 As Distance, ByVal op2 As Distance) As Boolean
Return (op1.Number >= op2.Number)
End Operator
Public Shared Operator <=(ByVal op1 As Distance, ByVal op2 As Distance) As Boolean
Return (op1.Number <= op2.Number)
End Operator
End Class
Public Sub ListDistances()
Dim distFrom As New Distance(10)
Dim distTo As New Distance(25)
Dim distStep As New Distance(4)
For dist As Distance = distFrom To distTo Step distStep
Debug.Write(dist.Number.ToString & " ")
Next
Debug.WriteLine("")
' Output: 10 14 18 22
End Sub