ASP performs several types of repetitive operations, called "looping". Loops are set of instructions used to repeat the same block of code till a specified condition returns false or true depending on how you need it. To control the loops you can use counter variable that increments or decrements with each repetition of the loop.
The two major groups of loops are For..Next and Do..Loop. The For...Next statements are best used when you want to perform a loop a specific number of times. The Do...Loop statements are best used to perform a loop an undetermined number of times. In addition, you can use the Exit keyword within loop statements.
For...Next loops are used when you want to execute a piece of code a set number of times. The syntax is as follows:
For counter = initial_value to finite_value [Step increment] statements Next
The For statement specifies the counter variable and its initial and finite values. The Next statement increases the counter variable by one. Optional the Step keyword allows to increase or decrease the counter variable by the value you specify.
Have a look at the very simple example:
<%@ language="vbscript" %> <% For i = 0 to 10 Step 2 'use i as a counter response.write("The number is " & i & "<br />") Next %>
The preceding example prints out even numbers from 0 to 10, the <br> tag puts a line break in between each value.
Next example generates a multiplication table 2 through 9. Outer loop is responsible for generating a list of dividends, and inner loop will be responsible for generating lists of dividers for each individual number:
The For Each...Next loop is similar to a For...Next loop. Instead of repeating the statements a specified number of times, the For Each...Next loop repeats the statements for each element of an array (or each item in a collection of objects).
The following code snippet creates drop-down list where options are elements of an array:
<% Dim bookTypes(7) 'creates first array bookTypes(0)="Classic" bookTypes(1)="Information Books" bookTypes(2)="Fantasy" bookTypes(3)="Mystery" bookTypes(4)="Poetry" bookTypes(5)="Humor" bookTypes(6)="Biography" bookTypes(7)="Fiction"
Dim arrCars(4) 'creates second array arrCars(0)="BMW" arrCars(1)="Mercedes" arrCars(2)="Audi" arrCars(3)="Bentley" arrCars(4)="Mini"
Sub createList(some_array) 'takes an array and creates drop-down list dim i response.write("<select name=""mylist"">" & vbCrLf) 'vbCrLf stands for Carriage Return and Line Feed For Each item in some_array response.write("<option value=" & i & ">" & item & "</option>" & vbCrLf) i = i + 1 Next 'repeat the code and move on to the next value of i response.write("</select>") End Sub
'Now let's call the sub and print out our lists on the screen Call createList(bookTypes) 'takes bookTypes array as an argument Call createList(arrcars) 'takes arrCars array as an argument %>
The Do...Loop is another commonly used loop after the For...Next loop. The Do...Loop statement repeats a block of statements an indefinite number of times. The statements are repeated either while a condition is True or until a condition becomes True. The syntax looks as follows:
Do [While|Until] condition statements Loop
Here is another syntax:
Do statements Loop [While|Until] condition
In this case the code inside this loop will be executed at least one time. Have a look at the examples:
The example below defines a loop that starts with i=0. The loop will continue to run as long as i is less than, or equal to 10. i will increase by 1 each time the loop runs.
<% Dim i 'use i as a counter i = 0 'assign a value to i
Do While i<=10 'Output the values from 0 to 10 response.write(i & "<br \>") i = i + 1 'increment the value of i for next time loop executes Loop %>
Now let's consider a more useful example which creates drop-down lists of days, months and years. You can use this code for registration form, for example.
The Exit keyword alters the flow of control by causing immediate exit from a repetition structure. You can use the Exit keyword in different situations, for example to avoid an endless loop. To exit the For...Next loop before the counter reaches its finite value you should use the Exit For statement. To exit the Do...Loop use the Exit Do statement.
Have a look at the example:
<% response.write("<p><strong>Example of using the Exit For statement:</strong><p>")
For i = 0 to 10 If i=3 Then Exit For response.write("The number is " & i & "<br />") Next
response.write("<p><strong>Example of using the Exit Do statement:</strong><p>")
i = 5 Do Until i = 10 i = i - 1 response.write("The number is " & i & "<br />") If i < 10 Then Exit Do Loop %>