Javatpoint Logo
Javatpoint Logo

VB.NET Do Loop

A Loop is used to repeat the same process multiple times until it meets the specified condition in a program. By using a loop in a program, a programmer can repeat any number of statements up to the desired number of repetitions. A loop also provides the suitability to a programmer to repeat the statement in a program according to the requirement. A loop is also used to reduce the program complexity, easy to understand, and easy to debug.

Advantages of VB.NET loop

  • It provides code iteration functionality in a program.
  • It executes the statement until the specified condition is true.
  • It helps in reducing the size of the code.
  • It reduces compile time.

Types of Loops

There are five types of loops available in VB.NET:

  • Do While Loop
  • For Next Loop
  • For Each Loop
  • While End Loop
  • With End Loop

Do While Loop

In VB.NET, Do While loop is used to execute blocks of statements in the program, as long as the condition remains true. It is similar to the While End Loop, but there is slight difference between them. The while loop initially checks the defined condition, if the condition becomes true, the while loop's statement is executed. Whereas in the Do loop, is opposite of the while loop, it means that it executes the Do statements, and then it checks the condition.

Syntax:

In the above syntax, the Do keyword followed a block of statements, and While keyword checks Boolean_expression after the execution of the first Do statement.

Flowchart of Do loop

VB.NET Do Loop

The above flow chart represents the flow of Do While loop. It is used to control the flow of statements, such that it executes the statement at least once before checking the While or Until condition. If the condition is true, the next iteration will be executed till the condition become false.

Example 1. Write a simple program to print a number from 1 to 10 using the Do While loop in VB.NET.

Do_loop.vb

Now compile and execute the above program by clicking on the Start button, it shows the following output:

VB.NET Do Loop

In the above program, the Do While loop executes the body until the given condition becomes false. When the condition becomes false the loop will be terminated.

Use of Until in Do Until Loop statement

In the VB.NET loop, there is a Do Until loop statement, which is similar to the Do While loop. The Do Statement executes as long as Until condition becomes true.

Example: Write a program to understand the uses of Do Until Loop in VB.NET.

Do_loop.vb

Output:

VB.NET Do Loop

In the above program, a Do Until loop is executed their statement until the given condition Until (i =10) is not meet. When the counter value of the variable i becomes 10, the defined statement will be false, and the loop will be terminated.

Nested Do While Loop Statement

In VB.NET, when we use one Do While loop inside the body of another Do While loop, it is called Nested Do While loop.

Syntax

Example 2: Write a simple program to use the Do While loop Statement in VB.NET.

Nest_Do_While.vb

Output:

VB.NET Do Loop

In the above example, each iteration of the outer loop also executes the inner loop repeatedly until the inner condition becomes false. When the condition of the outer loop becomes false, the execution of the outer and inner loop will be terminated.


Next TopicFor Next Loop




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA