How can you exit a for loop in VB?

In VB, you can use the Exit For statement to break out of a For loop. When the Exit For statement is executed, the loop will immediately stop, and the program will continue to execute the code after the loop. Here’s an example:

For i As Integer = 1 To 10
    If i = 5 Then
        Exit For
    End If
    Console.WriteLine(i)
Next

In the above code, the Exit For statement will exit the loop when the value of i is equal to 5 and will no longer execute the code inside the loop. As a result, the loop will only output the numbers 1, 2, 3, and 4.

bannerAds