●離開 Sub 副程式區塊,使用 Exit Sub:
●使用 Exit Function 離開 Function 副程式區塊:
●使用 Return 離開 Function 副程式區塊:
●透過 Exit For、Exit Do、Exit While 離開重複流程區塊:
●用 Continue For、Continue Do、Continue While 直接前往重複流程區塊的下一回:
●透過 Goto 直接跳躍至位置標籤處:
●多層重複執行區塊,透過 Goto 直接跳往最外層:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = ""
TextBox1.Text = "●離開 Sub 副程式區塊,使用 Exit Sub: Start"
Exit Sub
TextBox1.Text = TextBox1.Text & " End" '沒有執行
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & F()
End Sub
Function F() As Integer
F = 1
TextBox1.Text = TextBox1.Text & "Start"
F = 2
Exit Function
F = 3
TextBox1.Text = TextBox1.Text & "End"
F = 4
End Function
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox1.Text = ""
TextBox1.Text = TextBox1.Text & F1()
End Sub
Function F1() As Integer
F1 = 1
TextBox1.Text = TextBox1.Text & "Start"
F1 = 2
Return 99
F1 = 3
TextBox1.Text = TextBox1.Text & "End"
F1 = 4
End Function
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim I As Integer = 0
TextBox1.Text = ""
For I = 1 To 10
TextBox1.Text = TextBox1.Text & "For :" & I & " "
If I = 5 Then
Exit For
End If
Next
I = 0
Do Until I = 10
I = I + 1
TextBox1.Text = TextBox1.Text & "Do :" & I & " "
If I = 5 Then
Exit Do
End If
Loop
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim I As Integer = 0
TextBox1.Text = ""
For I = 1 To 10
TextBox1.Text = TextBox1.Text & "For :" & I & "-1 "
If I > 5 Then
Continue For
End If
TextBox1.Text = TextBox1.Text & "For :" & I & "-2 "
TextBox1.Text = TextBox1.Text & Chr(13) + Chr(10)
Next
TextBox1.Text = TextBox1.Text & Chr(13) + Chr(10)
TextBox1.Text = TextBox1.Text & Chr(13) + Chr(10)
I = 0
Do Until I = 10
I = I + 1
TextBox1.Text = TextBox1.Text & "Do :" & I & "-1 "
If I > 5 Then
Continue Do
End If
TextBox1.Text = TextBox1.Text & "Do :" & I & "-2 "
TextBox1.Text = TextBox1.Text & Chr(13) + Chr(10)
Loop
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
TextBox1.Text = "1" & Chr(13) + Chr(10)
TextBox1.Text = TextBox1.Text & "2" & Chr(13) + Chr(10)
TextBox1.Text = TextBox1.Text & "3" & Chr(13) + Chr(10)
GoTo Address0001
Address0002:
TextBox1.Text = TextBox1.Text & "4" & Chr(13) + Chr(10)
TextBox1.Text = TextBox1.Text & "5" & Chr(13) + Chr(10)
TextBox1.Text = TextBox1.Text & "6" & Chr(13) + Chr(10)
GoTo Address0003
Address0001:
TextBox1.Text = TextBox1.Text & "7" & Chr(13) + Chr(10)
TextBox1.Text = TextBox1.Text & "8" & Chr(13) + Chr(10)
GoTo Address0002
Address0003:
TextBox1.Text = TextBox1.Text & "9" & Chr(13) + Chr(10)
TextBox1.Text = TextBox1.Text & "10" & Chr(13) + Chr(10)
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim X As Long = 0
Dim Y As Long = 0
TextBox1.Text = ""
Do
X = X + 1
Do
Y = Y + 1
TextBox1.Text = TextBox1.Text & "X:" & X & ",Y:" & Y & Chr(13) + Chr(10)
If Y = 500 Then
GoTo Address0001
End If
Exit Do
Loop
Loop
Address0001:
End Sub
End Class
沒有留言:
張貼留言