While, Do While. Suppose in VB.NET we wish to continue looping until a condition is met. The number of iterations though may be unknown (as of yet).
Loop notes. With Do While, and While, we can loop indefinitely (and stop at a certain point). These loops are ways to continue iterating while one or more conditions are true.
An example. A Do While loop can have one or more conditions in its expression. Here we use 2 conditions. We continue iterating while "i" is positive and "z" is less than or equal to 8.
Then In each iteration, we change the values of these variables: "i" is decremented by 10 and "z" is incremented by 3.
Detail The loop ends because "z" exceeds 8. It is changed to 9 in the final iteration and then the loop terminates.
Also If the "z" condition in the loop expression was removed, the loop would continue until "i" was set to -10.
Module Module1
Sub Main()
' Locals used in Do While loop.
Dim i As Integer = 100
Dim z As Integer = 0
' Loop.Do While i >= 0 And z <= 8
Console.WriteLine("i = {0}, z = {1}", i, z)
i = i - 10
z = z + 3
Loop
End Sub
End Modulei = 100, z = 0
i = 90, z = 3
i = 80, z = 6
Do loop. With Do we can loop infinitely (or indefinitely). This loop gets a random number on each iteration. If the number is even, it uses "Exit Do" to stop the loop.
However This style of loop can be used to run until a certain condition is met. This can even be an infinite loop.
Tip We can avoid the "While true" condition for an infinite loop, and just use a Do Loop.
Module Module1
Sub Main()
Dim random As New Random()
' Enter a loop.Do' Get random number.
Dim number As Integer = random.Next
' Print random number.
Console.WriteLine(number)
' Exit loop if we have an even number.
If number Mod 2 = 0 Then
Exit Do
End If
Loop
End Sub
End Module1315809053
1322882256
While versus Until. In VB.NET we can use a Do Until loop to mean "continue until the condition is matched." A Do While loop can be used in the same way.
Here We use the "not equals" operator with a Do While loop to continue until an array element is equal to 30.
And We show a Do Until loop that has the same effect as the Do While loop. Use whichever is clearest in the program.
Module Module1
Sub Main()
Dim index As Integer = 0
Dim array() As Integer = New Integer() {10, 20, 30, 40}
' Use "not equals" operator.Do While array(index) <> 30
Console.WriteLine("[WHILE] NOT 30: {0}", array(index))
index += 1
Loop
' Use "do until" loop.
index = 0
Do Until array(index) = 30
Console.WriteLine("[UNTIL] NOT 30: {0}", array(index))
index += 1
Loop
End Sub
End Module[WHILE] NOT 30: 10
[WHILE] NOT 30: 20
[UNTIL] NOT 30: 10
[UNTIL] NOT 30: 20
While example. Next, we can use a "While" loop without the "Do" keyword. While-loops are useful for cases where we do not know beforehand how many iterations will run.
Detail The two statements in the While-loop's body are executed repeatedly until that condition evaluates to false.
Tip You can also use the "And" operator to put 2 conditions in the While-loop.
Note This will result in a more complex program, but sometimes multiple conditions are necessary.
Module Module1
Sub Main()
Dim i As Integer = 0
While i < 100
Console.WriteLine(i)
i += 10
End While
End Sub
End Module0
10
20
30
40
50
60
70
80
90
Discussion. How can you determine which loop is best for your VB.NET program? The Do While construct is sometimes most convenient, particularly in less common situations.
Warning Please be aware that this syntax form can easily lead to infinite loops.
And The For Each loop probably leads to the fewest errors, but it cannot be used in all cases.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 16, 2023 (edit link).