C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Caution: This syntax can easily result in an infinite loop if you are not careful.
And: We find that Do Until has all the same problems as While and Do While in this respect.
VB.NET program that uses Do Until
Module Module1
Sub Main()
Dim i As Integer = 0
' Example Do Until loop.
Do Until i = 10
Console.WriteLine("Do Until: {0}", i)
i += 1
Loop
Console.WriteLine()
i = 0
' Equivalent Do While loop.
Do While i < 10
Console.WriteLine("Do While: {0}", i)
i += 1
Loop
End Sub
End Module
Output
Do Until: 0
Do Until: 1
Do Until: 2
Do Until: 3
Do Until: 4
Do Until: 5
Do Until: 6
Do Until: 7
Do Until: 8
Do Until: 9
Do While: 0
Do While: 1
Do While: 2
Do While: 3
Do While: 4
Do While: 5
Do While: 6
Do While: 7
Do While: 8
Do While: 9
Note: The Do While syntax is more expected for developers used to C# or C-like languages.
Conceptually: The Do Until syntax could be thought of as a "Do While Not" syntax. It changes a Do While loop to evaluate while False.
And: If you looping over elements of a collection, it is probably best to use a For-Each loop.
For Each, For