VB.NET Do While Loop Examples (While)Use the Do While and While loops. Keep looping as long as a condition remains True.
dot net perls

While, Do While. Suppose we wish to continue looping until a condition is met. The number of iterations though may be unknown (as of yet).

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.

Do Until

For Each, For

An example. A Do While loop can have one or more conditions in its expression. Here we use 2 conditions. We continue iterating while the variable "i" is positive and "z" is less than 20.

Then In each iteration, we change the values of these variables: "i" is decremented by 10 and "z" is incremented by 3.

End The loop ends because "z" exceeds 20. It is changed to 21 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.

VB.NET program that uses Do While loop
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 <= 20 Console.WriteLine("i = {0}, z = {1}", i, z) i = i - 10 z = z + 3 Loop End Sub End Module
i = 100, z = 0 i = 90, z = 3 i = 80, z = 6 i = 70, z = 9 i = 60, z = 12 i = 50, z = 15 i = 40, z = 18

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.

VB.NET program that uses Do
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 Module
1315809053 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.

VB.NET program that uses Do While, Do Until
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.

Program Here an Integer i is used as the index variable. The While-loop has one condition: i < 100.

Statements 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.

VB.NET program that uses While-loop
Module Module1 Sub Main() Dim i As Integer = 0 While i < 100 Console.WriteLine(i) i += 10 End While End Sub End Module
0 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.

Discussion, continued. The For Each loop probably leads to the fewest errors, but it cannot be used in all cases. The next best loop is For.

Tip For makes the terminating conditions and iteration expression clear. This is a clear advantage over Do-while.

Note Thanks to Christoph Hafner for simplifying a Do While true loop into a Do loop.

Notes, While. How is the Do-While loop different from the While construct? The 2 loops function the same way, but have slightly different syntax.

Info Do-While ends with the Loop statement. The While construct ends with "End While". Other than that, they are equivalent.

Notes, infinite loops. Perhaps the biggest worry when dealing with While-loops in any programming language is an infinite loop. Sometimes a termination condition will never be reached.

Tip In a For-loop, you are less likely to remove the increment expression, making it a safer construct. For-Each is even safer.

A summary. Do While in VB.NET provides a way to specify a loop that will continue until the exit condition is met. This can lead to infinite loops unless you are careful.

In writing loops, we try to choose the format that is most clear. A do-while construct is sometimes the clearest way of expressing intent. While is equivalent to the Do While construct.

© 2007-2021 sam allen. send bug reports to info@dotnetperls.com.