Home
Search
Do While Loop Examples (While)Use the Do While and While loops. Keep looping as long as a condition remains True.
VB.NET
This page was last reviewed on Nov 17, 2022.
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.
Do Until
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.
End 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 Module
i = 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 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.
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.
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.
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.
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.
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 Nov 17, 2022 (simplify).
Home
© 2007-2022 sam allen.
see site info on the changelog.