Home » VB.NET While End Loop

VB.NET While End Loop

by Online Tutorials Library

VB.NET While End Loop

The While End loop is used to execute blocks of code or statements in a program, as long as the given condition is true. It is useful when the number of executions of a block is not known. It is also known as an entry-controlled loop statement, which means it initially checks all loop conditions. If the condition is true, the body of the while loop is executed. This process of repeated execution of the body continues until the condition is not false. And if the condition is false, control is transferred out of the loop.

Syntax:

Here, condition represents any Boolean condition, and if the logical condition is true, the single or block of statements define inside the body of the while loop is executed.

Flow Diagram of the While End Loop in VB.NET

VB.NET While End Loop

As we know, the While End loop is an entry-controlled loop used to determine if the condition is true, the statements defined in the body of the loop are executed, and the execution process continues till the condition is satisfied. Furthermore, after each iteration, the value of the counter variable is incremented. It again checks whether the defined condition is true; And if the condition is again true, the body of the While loop is executed. And when the condition is not true, the control transferred to the end of the loop.

Example: Write a simple program to print the number from 1 to 10 using while End loop in VB.NET.

while_number.vb

Output:

VB.NET While End Loop

In the above example, while loop executes its body or statement up to the defined state (i <= 10). And when the value of the variable i is 11, the defined condition will be false; the loop will be terminated.

Example 2: Write a program to print the sum of digits of any number using while End loop in VB.NET.

Total_Sum.vb

Output:

VB.NET While End Loop

Nested While End Loop

In VB.NET, when we write a While End loop inside the body of another While End loop, it is called Nested While End loop.

Syntax

Write a program to understand the Nested While End loop in VB.NET programming.

Nest_While.vb

Output:

VB.NET While End Loop

In the above example, at each iteration of the outer loop, the inner loop is repeatedly executed its entire cycles until the inner condition is not satisfied. And when the condition of the outer loop is false, the execution of the outer and inner loop is terminated.


You may also like