Loop Statements in VB.NET

In this article I will show looping statements FOR LOOP, DO...UNTIL LOOP, DO..LOOP WHILE and WHILE...END WHILE loop in Visual Basic .NET.
  • 8081

Visual Basic allows a procedure to be repeated many times as long as the processor until a condition or a set of conditions is fulfilled. This is generally called looping . Looping is a very useful feature of Visual Basic because it makes repetitive works easier. There are several type of looping structure in Visual Basic.NETLoop allows you to repeat an action for a number of times or until a specified condition is matched.

FOR...Next loop

A For loop iterates a certain number of times, the value of the counter variable changing each iteration.

A for loop has the following syntax:

For counter = start to end [step increment]
statements
Next

In executing a FOR..NEXT loop, Visual Basic completed the following step

  • Sets counter equal to start.
  • Tests to see if counter is greater than end, so it exit the loop if counter is less than end. if it is, it exits the loop.
  • Executes the statement in block.
  • Repeats the statements

Example shows the FOR loop:

Dim i As Integer

For i = 1 To 10

Console.WriteLine(i)

Next

Do...UNTIL Loop:

A Do...Loop Until loop is a loop that runs until the loop's condition is true, the condition being checked after each iteration of the loop.

Sample:

Do
statements
Loop Until condition

Example shows the DO..Until loop:

' DO..UNTIL loop in VB.NET

            Do Until i > 10

                Console.WriteLine(i)

                i = i + 1

            Loop

Do...Loop While loop:

A Do...Loop While loop runs until the loop's condition becomes false. Its condition is checked after each iteration of the loop.

Sample:

Do
statements
Loop While condition

Example Shows the DO while Loop: 

'DO..LOOP WHILE in VB.NET

            Do

                Console.WriteLine(i)

                i = i + 1

            Loop While i < 10      

 While...End While loop:

The While...End  While loop executes a block of statement as long as condition is true.

The While loop has the following syntax:

While condition
statement-block
End While

Example shows the While...End While loop:

' While...End While loop in VB.NET

            While i < 10

                Console.WriteLine(i)

                i = i + 1

            End While

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.