Mega Code Archive

 
Categories / VB.Net Tutorial / Statements
 

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