Using the Exit keyword in repetition structures : Exit « Statements « VB.Net Tutorial






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








4.13.Exit
4.13.1.Using the Exit keyword in repetition structures
4.13.2.Do Loop with Exit Do
4.13.3.Exit For