Menu
ASP Loop Control Structures

A loop control structure uses one or more conditional statements to control how many times the same block of code is executed. The most basic loop is the For...Next control structure.

<%
For i = 0 To 5
  response.write("i = " & i & "<br />")
Next
%>

The For statement specifies a counter variable, usually i but any variable name can be used, and the variable's start and end values. The Next statement increases the value of the variable by 1 each time though the loop until it reaches the end value, then the loop exits. So you have control over the exact number of times the code will be executed.

<%
For i = 0 To 10 Step 2
  response.write("i = " & i & "<br />")
Next
%>

In the example above, the Step keyword is used to set an amount that the counter variable will be advanced each time though the loop. Here, the value of the variable i will be increased 2 each time through the loop.

<%
For i = 10 To 2 Step -2
  response.write("i = " & i & "<br />")
Next
%>

Sometimes the actual value of the counter variable might be used in the code and, you might want a value that decreases, or decrements, on each loop through the code. To cause the counter variable to decrease you can use a negative Step value. In the example, the counter variable is decreased by 2, each time through the loop.

<%
For i = 1 To 10
  If j = 5 Then Exit For
  response.write("j = " & i & "<br />")
Next
%>

Sometimes you might need to terminate a loop based on some condition other than the value of the loop's counter variable. To do that you can put a conditional statement (If - Then) within the loop, as shown above, and use the Exit keyword to jump out of the loop.

Do... Loop

Another type of loop control structure is the Do... loop. If the number of times to run a block of code depends upon the value of some result, and you don't know the number of times you need to execute the block of code, then you will need a Do.. loop. There are two types of Do... loops, the Do... while and the Do... until.

<%
Dim i
i = 0
Do While i < 10
  response.write("i = " & i & "<br />")
  i = i + 1
Loop
%>

The code inside the loop will be executed until the value of i = 10. If the value of i is initlaized to 10 or higher, the code in the loop will never be executed.

<%
Dim i
i = 20
Do
  response.write("i = " & i & "<br />")
  i = i - 1
Loop While i > 10
%>

Because the conditional statement is at the end of the loop above, the code inside the loop will be executed at least one time, even if i is less than 10. The Do... While loop control structure repeats the loop until the test condition is false.

<%
Dim i
i = 1
Do Until i = 10
  response.write("i = " & i & "<br />")
  i = i + 1
Loop
%>

The code inside this loop will be repeated until the value of i is equal to 10. If i is initialized to 10, the code inside the loop will never be executed.

<%
Dim i
i = 10
Do
  response.write("i = " & i & "<br />")
  i = i + 1
Loop Until i = 10
%>
Because the conditional statement is at the end of the loop above, the code inside the loop will be executed at least one time, even if i is equal to 10. The Do... Until loop control structure repeats until the test condition is true.

<%
Dim i
i = 0
Do Until i = 10
  response.write("i = " & i & "<br />")
  i = i + 1
  If j < 10 Then Exit Do
Loop
%>

Sometimes you might need to terminate a loop based on some condition other than the value of the loop's counter variable. To do that you can put a conditional statement (If - Then) within the loop, as shown above, and use the Exit Do keywords to jump out of the loop.

While... Wend Loop

The While... Wend loop control structure executes a conditional statement before each execution of a block of code. If the conditional statement returns true, the block of code is executed. If the conditional statement returns false, the loop is exited.

<%
Dim i
i = 10   
While i < 15
  response.write("i = " & i & "<br />")
  i = i + 1
Wend
%>

While loop exits when the value of i equals 15.

Most programmers execute code, within the loop. on the variable used in the loop control conditional statement. This often results in the creation of an infinite loop. So make sure you know what you're doing if you use the While...Wend control structure.

For Each... Next

The number of elements in an array or a collection can be changed. One way to execute a block of code for each item in an array or collection without knowing the number of items is to use the For Each... Next loop control structure.

<%
Dim power(), i
power(0) = "1/8 Watt"
power(1) = "1/4 Watt"
power(2) = "1/2 Watt"

For Each i In power
  response.write(power(i) & "<br />")
Next
%>

In the example above, the array does not have a fixed length. To display the value of every element in the array, the code in the For Each... Next loop executes for each element in the power array.