Use Do...Loop
to repeat a block of statements While
or Until
a condition is true, checking the condition either at the beginning or at the end of the loop.
Dim x As Integer = 0
Do
Console.Write(x & " ")
x += 1
Loop While x < 10
or
Dim x As Integer = 0
Do While x < 10
Console.Write(x & " ")
x += 1
Loop
0 1 2 3 4 5 6 7 8 9
Dim x As Integer = 0
Do
Console.Write(x & " ")
x += 1
Loop Until x = 10
or
Dim x As Integer = 0
Do Until x = 10
Console.Write(x & " ")
x += 1
Loop
0 1 2 3 4 5 6 7 8 9
Continue Do
can be used to skip to the next iteration of the loop:
Dim x As Integer = 0
Do While x < 10
x += 1
If x Mod 2 = 0 Then
Continue Do
End If
Console.Write(x & " ")
Loop
1 3 5 7 9
You can terminate the loop with Exit Do
- note that in this example, the lack of any condition would otherwise cause an infinite loop:
Dim x As Integer = 0
Do
Console.Write(x & " ")
x += 1
If x = 10 Then
Exit Do
End If
Loop
0 1 2 3 4 5 6 7 8 9