Exit
語句將過程或塊中的控制立即轉移到過程呼叫或塊定義之後的語句中。它終止迴圈,過程,嘗試塊或從呼叫的選擇塊。
如果使用巢狀迴圈(即在一個迴圈內部有另外一個迴圈),則Exit
語句將停止最內層迴圈的執行,並開始執行塊之後的下一行程式碼。
Exit
語句的語法是:
Exit { Do | For | Function | Property | Select | Sub | Try | While }
流程圖
Module loops
Sub Main()
' local variable definition '
Dim a As Integer = 10
' while loop execution '
While (a < 20)
Console.WriteLine("value of a: {0}", a)
a = a + 1
If (a > 15) Then
'terminate the loop using exit statement '
Exit While
End If
End While
Console.ReadLine()
End Sub
End Module
執行上面範例程式碼,得到以下結果 -
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15