C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
Output
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
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
Output
1315809053
1322882256
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
Output
[WHILE] NOT 30: 10
[WHILE] NOT 30: 20
[UNTIL] NOT 30: 10
[UNTIL] NOT 30: 20
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
Output
0
10
20
30
40
50
60
70
80
90
Warning: Please be aware that this syntax form can easily lead to infinite loops.
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.
Info: Do-While ends with the Loop statement. The While construct ends with "End While". Other than that, they are equivalent.
Tip: In a For-loop, you are less likely to remove the increment expression, making it a safer construct. For-Each is even safer.