Page 83 -
P. 83
前測式 Do-Loop 迴圈 第四章 程式的流程控制
Dim index As Integer = 0 後測式 Do-Loop 迴圈
Do While index <= 10
Dim index As Integer = 0
Debug.Write(index & " ") Do
index += 1
Loop Debug.Write(index & " ")
index += 1
Dim index As Integer = 0 Loop While index <= 10
Do Until index > 10
Dim index As Integer = 0
Debug.Write(index & " ") Do
index += 1
Loop Debug.Write(index & " ")
index += 1
Loop Until index > 10
另外,Do-Loop 迴圈內敍述中,我們可以使用 Exit Do 陳述式可以提供結束 Do-Loop 的
迴圈執行。Exit Do 會立即將控制權轉移至 Loop 陳述式隨後的陳述式。Exit Do 通常會配合
使用 If...Then...Else 結構。如果偵測到一個條件(例如錯誤值或終止要求),而該條件會使迴
圈不用繼續重複執行,則您可能會想要結束迴圈。如下範例:同上述例子輸出 0 到 10 的數字。
Dim index As Integer = 0
Do While index <= 100
If index > 10 Then
Exit Do
End If
Debug.Write(index & " ")
index += 1
Loop
79