vbnet
  1. vbnet-do-loop

Control Statements in VB.NET Do Loop

The Do Loop is a type of control statement in VB.NET that executes a block of code repeatedly until a specified condition becomes false. In this page, we will discuss the syntax, example, output, explanation, use, important points, and summary of control statements in the VB.NET Do Loop.

Syntax

Here's the syntax for the Do Loop in VB.NET:

Do [ While | Until ] condition
    [ statements ]
[ Continue Do ]
[ Exit Do ]
    [ statements ]
Loop
  • The condition that is specified after the Do While or Do Until determines whether the loop should be executed again.
  • The statements inside the loop will be executed repeatedly while the condition is true.
  • The Continue Do statement skips the rest of the current iteration and moves on to the next one.
  • The Exit Do statement terminates the loop immediately.

Example

Here's an example of a Do Loop statement that prints numbers from 1 to 5.

Dim count As Integer = 1

Do While count <= 5
    Console.WriteLine(count)
    count += 1
Loop

Output

The output of the code above will be:

1
2
3
4
5

Explanation

The Do While statement is used in this example to execute the loop code while the "count" variable is less than or equal to 5. Inside the loop, the Console.WriteLine statement is used to print the current value of the "count" variable to the output console. The "count" variable is then incremented after each iteration of the loop.

Use

The Do Loop control statement is useful for repeating a block of code until a certain condition is met. It's commonly used in situations where you need to iterate through a collection of data, perform a task multiple times, or wait for a certain event to occur.

Important Points

  • The Do While statement executes the loop code while the specified condition is true.
  • The Do Until statement executes the loop code until the specified condition is true.
  • The Continue Do statement skips the rest of the current iteration and moves on to the next one.
  • The Exit Do statement terminates the loop immediately.
  • The condition specified after the Do While or Do Until must eventually become false to stop the loop from running indefinitely.

Summary

In this page, we discussed the syntax, example, output, explanation, use, important points, and summary of Control Statements in VB.NET Do Loop. Remember that the Do Loop control statement is useful for repeating a block of code until a certain condition is met. Use the Continue Do statement to skip the rest of the current iteration and the Exit Do statement to terminate the loop immediately.

Published on: