VB.NET Do Loop Until

The Article is about the use of VB.NET Do Loop Until.

The VB.NET Do Loop Until, like the for loop and while loop, has a condition which defines it’s termination point. However, unlike them it checks its condition at the bottom of the loop instead of the top. It can also be called a post condition loop.

This results in the Do Loop Until loop always running once no matter the condition, unlike the while loop, which may not run at all if the condition is false.

Syntax

Below is the general syntax for the VB.NET Do Loop Until loop.

Do
  ' statements
Loop Until (condition)

Example

A basic example of the VB.NET Do Loop Until loop. Note that while the loop goes up to 5, the value 5 is not actually displayed. This is because the loop terminates itself before the print statement is reached.

Dim x as Integer

Do 
    Console.writeline(x)
    x = x + 1
Loop Until (x < 5)
0
1
2
3
4

As of this point, the Do Loop Until loop still seems to be identical to the while loop. We’ll now show an example where both of them act differently.


Example 2

The code in the example below takes input from the user using the Console.readline() statement. The input from the user determines the fate of the Do Loop Until loop.

Module Module1
       Sub Main()
		Dim x As Integer
		Dim sum As Integer = 0
		
		Console.WriteLine("Input a negative number to exit")
		
		Do 
		  x = Console.ReadLine() 
		  sum = sum + x     
		  Console.WriteLine(sum)		
	        Loop Until(x > 0)
		
	        Console.WriteLine(sum)
       End Sub
End Module

The above code is designed to keep taking input from the user, adding them up in the variable sum until a negative number is input to terminate the loop.

The use of the Do Loop Until loop is important here as the code must run once so that the value of x can be obtained to be matched against the condition. This is contrast to the while loop, which would first check the condition and throw an error since x has no value at that point.


Using Multiple Statements

Using the and logic operator we can connect two conditions together. Chaining two conditions together with and ensures the loop only runs when both conditions are true, and will terminate if any of them becomes false.

Module Module1
        Sub Main()		
		Dim x as Integer = 0;	
		Dim y As Integer = 0;
		
		Do  
		    x = x + 1
		    y = y + 2
		    Console.WriteLine("X: " + x + " " + "Y: " + y)  
		Loop Until (x < 5 And y < 6);    		     			
         End Sub
End Module

The above loop stops after the value of y reaches 6, even though x was still less than 5.

You can use other operators like the or (One of the two conditions must be true) and xor (Only one condition must be true) to change things up. See the VB.NET Operators guide to learn more.


Loop control statements

There are several loop control statements in VB.NET, that as the name implies, can be used to manipulate the Do Loop Until loop during it’s execution.

Continue statement

The continue statement, when encountered will skip any remaining lines of code in the current iteration of the loop and skip to the beginning of the next iteration.

We’ll be modifying a portion of the code from one of the previous examples here. In this example, we want to write a program that takes the sum of all the positive odd numbers.

Do
     x = Console.readline() //takes input from user
     If (x % 2 = 0) Then Continue Do
     sum = sum + x  
     Console.writeline(sum)
Loop Until x > 0

When an even number is input and evaluated by the mod function, it’s remainder will be zero. Since the if condition came true, the continue statement will activate, skip the next two lines and go back to the start of the loop. Only odd numbers will be allowed through.

Exit statement

The exit statement when called, terminates a loop during it’s execution. It is an effective way of forcing your loop to stop if some unexpected outcome has been encountered.

Module Module1
       Sub Main()		
		Dim x as Integer
		Dim count as Integer		

		Do
		  x = Console.readline()
		  If x < 0 Then
		     Console.writeline("Please input positive numbers")
		     Exit Do
		  End If
		  count = count + 1
		  Console.writeline(x)
	        Loop Until (count < 5)		
        End Sub
End Module

The above code runs for a total of 5 times, taking input of positive numbers from the user. To ensure the user doesn’t input a negative number, we use a if statement that leads to a Exit statement and an error message. When the user inputs a negative number, the error message is printed and the loop terminates due to the Exit statement.

5
5
2
2
-6
Please input positive numbers

This marks the end of the VB.NET Do Loop Until loop article. Any suggestions or contributions for CodersLegacy are more than welcome. You can ask any relevant questions in the comments section below.