VB.Net Tutorial/Statements/Exit
Версия от 16:40, 26 мая 2010; (обсуждение)
Do Loop with Exit Do
Option Strict On
Imports System
Module Module1
Sub Main( )
Dim counterVariable As Integer = 0
Do
Console.WriteLine("counterVariable: {0}", counterVariable)
counterVariable = counterVariable + 1
If counterVariable > 9 Then
Exit Do
End If
Loop
End Sub "Main
End Module
counterVariable: 0 counterVariable: 1 counterVariable: 2 counterVariable: 3 counterVariable: 4 counterVariable: 5 counterVariable: 6 counterVariable: 7 counterVariable: 8 counterVariable: 9
Exit For
public class Test
public Shared Sub Main
Dim j As Integer
For i As Integer = 1 To 3
j = 0
Do While j < 3
j += 1
For k As Integer = 1 To 3
Dim test1 As Boolean = k = 2
If test1 Then Exit For " Exits the For K loop.
Dim test2 As Boolean = i = j
If test2 Then Exit Do " Exits the Do.
Console.WriteLine(i & ", " & j & ", " & k)
Next k
Loop
Next i
End Sub
End class
2, 1, 1 3, 1, 1 3, 2, 1
Using the Exit keyword in repetition structures
Module Tester
Sub Main()
Dim output As String
Dim counter As Integer
For counter = 1 To 10
If counter = 3 Then
Exit For
End If
Next
output = "counter = " & counter & _
" after exiting For/Next structure" & vbCrLf
Do Until counter > 10
If counter = 5 Then
Exit Do
End If
counter += 1
Loop
output &= "counter = " & counter & _
" after exiting Do Until/Loop structure" & vbCrLf
While counter <= 10
If counter = 7 Then
Exit While
End If
counter += 1
End While
output &= "counter = " & counter & _
" after exiting While structure"
Console.WriteLine(output)
End Sub
End Module
counter = 3 after exiting For/Next structure counter = 5 after exiting Do Until/Loop structure counter = 7 after exiting While structure