難道......迴圈
使用 Do...Loop
重複一個語句塊 While
或 Until
條件為真,在迴圈的開頭或結尾檢查條件。
Dim x As Integer = 0
Do
Console.Write(x & " ")
x += 1
Loop While x < 10
要麼
Dim x As Integer = 0
Do While x < 10
Console.Write(x & " ")
x += 1
Loop
0 1 2 3 4 5 6 7 8 9
Dim x As Integer = 0
Do
Console.Write(x & " ")
x += 1
Loop Until x = 10
要麼
Dim x As Integer = 0
Do Until x = 10
Console.Write(x & " ")
x += 1
Loop
0 1 2 3 4 5 6 7 8 9
Continue Do
可用於跳轉到迴圈的下一次迭代:
Dim x As Integer = 0
Do While x < 10
x += 1
If x Mod 2 = 0 Then
Continue Do
End If
Console.Write(x & " ")
Loop
1 3 5 7 9
你可以使用 Exit Do
終止迴圈 - 請注意,在此示例中,缺少任何條件都會導致無限迴圈:
Dim x As Integer = 0
Do
Console.Write(x & " ")
x += 1
If x = 10 Then
Exit Do
End If
Loop
0 1 2 3 4 5 6 7 8 9