VB.NET While loop



The Article is about the use of VB.NET while loop.

The VB.NET while loop comprises of two components, a condition and a statement(s). The statement may be a single statement or even a group of statements, and the condition defines the condition that controls the loop. The condition may be any valid Boolean expression that returns a True or False value.

The loop repeats while the given condition holds true. When the condition becomes false, the loops terminates and program flow passes on to the line immediately after the loop.

The while loop has a similar counterpart called the do while loop.

Syntax

Below is the general code for creating a while loop. The while loop first checks the condition, and then decides what to do. If the condition proves to be false, the instructions in the code block will not be executed.

While (condition) 
   ' Instructions be executed
End While

Example

A basic example of the VB.NET While loop. Other supporting code was removed to improve readability and keep the code short.

Dim x As Integer

While (x < 5) 
    Console.WriteLine(x)
    x = x + 1
End While
0
1
2
3
4

As shown above, the while loop printed out all the numbers from 0 to 4. The loop terminated once the value of x reached 5, before the output statement was even reached. Hence 5 was not included in the output.


Using multiple conditions

Using the and operator && we can connect two conditions together. Chaining two conditions together with && 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
	
		While (x < 5 And y < 6) 
		    x = x + 1
		    y = y + 2	    
		    Console.WriteLine("X: " + x + " " + "Y: " + y)  
		End While	
	End Sub
End Module
X: 1 Y: 2
X: 2 Y: 4
X: 3 Y: 6

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 and xor to change things up. See the VB.NET Operators guide to learn more.


Loop control statements

There are several loop control in VB.NET, that as the name implies, can be used to manipulate the do while 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.

Dim x As Integer

While (x < 5) 
   x = x + 1
   If (x = 3) Then Continue While
   Console.WriteLine(x)
End While	
1
2
4
5

Since the continue statement is called when the value of x is equal to three, the Console.WriteLine function doesn’t get the chance to execute, and thus the value of x not printed to screen.

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

Dim x As Integer

While (x < 5) 
   x = x + 1
   If (x = 3) Then Exit While
   Console.WriteLine(x)
End While
1
2

The value of x in the above code does not go past 3 , since as soon as the value of x increments itself to the value 3, the loop terminates.


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

Leave a Reply

Your email address will not be published. Required fields are marked *