A loop is a technique used to repeat
an action. The Visual Basic language provides various techniques and
keywords to perform such actions called loops.

The For...To...Next Loop

Imagine you have a series of numbers you must visit to perform an action on
each. To assist you with this, the Visual Basic language provides a mechanism created with the For...Next expression. One of the loop counters you can use is For...To...Next.
Its formula is:

Forcounter=startToendstatement(s)Next

You must use a variable by which the counting would proceed.
You can first declare the variable or you can directly use it after the For keyword
and you must specify its initial value there as the start point. Here is
an example:

<%
Dim i As Integer
. . .
For i = 1 To . . .
Next
%>

After the To keyword,
specify the last value of the counting as the end point. The value being
counted can be used as the index of a collection. In this case, when a
value is passed to the name of the collection, it produces the object. The
result can be a regular item of the collection. The loop ends with the Next keyword. Here are two examples:

The regular formula of a loop increments the counting by
1 at the end of each statement. If you want to control how the
incrementing processes, you can set your own, using the Step option.
The formula to follow is:

Forcounter=startToendStepincrementstatement(s)Next

You can set the incrementing value to your
choice. If the value of increment is positive, the counter will be
added to its value. Here is an example:

In some cases, you may want to exit a conditional statement or a loop before its end. To assist with with this, the Visual Basic language provides the Exit keyword. It can be used in the body of a procedure or a loop.

Exiting a Procedure

Exiting a procedure consists of interrupting its flow and jumping to the End line of the procedure. To do this anywhere in the body of a procedure, type Exit Sub. Here is an example:

You can also exit a For loop. To do this, in the section where you want to stop, type Exit For. This would stop the flow of the loop and jump to the line immediately after the Next keyword. This feature of the Visual Basic language is very valuable when looking for a specific value or object in a collection. Here is an example:

The Visual Basic language presents many variations of loops. They combine the Do and the Loop
keywords.

The Do...Loop While Loop

One of the formulas to perform a loop uses the Do... Loop While approach. The formula to follow is:

Dostatement(s)Loop Whilecondition

The statement(s) would execute first. Then the condition would be checked. If the condition
is true, then the statement(s) would execute again. The check-execution
routine would continue as long as the condition is true. If/Once the condition becomes false, the statement will not execute anymore and the code will move beyond the loop. Here is an example:

As you may guess already,
the condition must provide a way for it to be true or to be false.
Otherwise, the looping would execute continually.

The Do...Loop Until Loop

An alternative to the Do... Loop While uses the following formula:

Dostatement(s)Loop Untilcondition

Once again, the statement(s) section executes first. After executing the statement(s), the
condition is checked. If the condition is true, the statement(s) section executes again. This will continue until the condition becomes false.
Once the condition becomes false, the loop stops and the flow continues with the section under the Loop Until
line. Here is an example:

The Visual Basic language provides another loop option that use the Do While... Loop
expression. Its formula is:

Do Whileconditionstatement(s)Loop

This time, the condition id checked first. If
the condition is true, then the statement(s) execute(s). Then the condition is checked again. If the Condition is false, or once/when the condition becomes
false, the statement(s) section is(are) skipped and the flow continues with the code below the Loop keyword. Here is an example: