Visual Basic .NET/Loop statements

There are several kinds of looping structures in VB.NET.

Loops allow you to repeat an action for a number of times or until a specified condition is reached.

Do...Loop 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...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 Until...Loop loop
A Do Until...Loop loop is almost the same as a Do...Loop Until loop. The only difference is that it has the condition's check at the beginning of each iteration of the loop.

Sample:

Do While...Loop loop
Likewise, a Do While...Loop loop is almost the same as a Do...Loop While loop. And like the Do Until...Loop loop, the condition's check is at the beginning of each iteration.

Sample:

For x as Integer = 1 To 10 console.writeline("x")

For loop
A For loop iterates a certain number of times, the value of the counter variable changing each iteration. The For loop is the most well-known looping statement and useful in many programs. A For loop looks like this this: will loop 10 times, since on the first iteration, a would equal 1, the second iteration 2, etc.

A For loop such as this one: will loop 10 times, but the counter starts at 10 and goes down to 1.

A For loop like this one: will also loop 10 times. The counter would start at 11 then stops when it reaches 20.

For Each loop
A For Each loop iterates over every index of an array or other iterable object. It requires an object to iterate through, and a variable to hold an index's contents. The object to iterate must implement the  or   interfaces. The implementation of  by   allows arrays to be used as an object, because   inherits from.

Sample: