Looping controls

If we want to execute our statements more than once, should we write our statements as many times we need to execute them? No, rather use a looping structure.

What is looping?

A looping is a control structure that executes statements multiple times. Looping is a way of executing statements more than time. On this web page, you will learn the DO...LOOP and the FOR...LOOP.


The DO.LOOP has two forms. The notable distinction between the two is testing of the loop condition (see the condition logic section to learn how to construct conditions using relational operators). In the first loop that we will discuss has the condition checked first. The syntax for this DO...WHILE...LOOP is

DO WHILE expression

The DO, WHILE, and LOOP are the keywords of VBScript. The loop starts with the keyword DO. After the WHILE keyword condition(s) is listed. The condition controls the number of times the loop will be executed. Then, statements or code we want to execute are listed after the condition and before the keyword LOOP. The keyword LOOP begins the execution of loop.

Let's start with an example that will print numbers 1 through 5 with a DO...WHILE loop:

DIM intCounter
intCounter = 1
Do while (intCounter <= 5)
Response.write intCounter & " "
intCounter = intCounter + 1

The first line above declares a variable called intCounter. The second line sets the value of this variable to 1. The third line says check the value of intCounter; if the value is less than or equal to 5, then, execute the code inside the loop. We have two statements inside the loop. The Response.write intCounter & " " statement prints the current value of intCounter. The intCounter = intCounter + 1 statement adds 1 to intCounter, each time the loop is executed. To sum up, we start intCounter with 1 and each time we print the value of intCounter inside the loop, the value of intCounter is updated by 1. The value of intCounter will reach 5 after the code inside the loop has executed five times. When the value of intCounter becomes 6, the loop will stop to execute.

The following code shows the output of the above code:

1 2 3 4 5

In the above loop example the condition is tested first. This means if the condition fails, then, statements inside the loop will not be executed. The second form of the DO...LOOP guarantees that statements of the loop will be executed at least once. This loop has the following form:

LOOP WHILE condition

This version is very similar to the first version of the previously mentioned DO...WHILE loop except that statements are listed first before the condition. Thus the statements will executed at least once before the condition the checked. Let's do the top example with the DO...LOOP to print number 1 through 5:

DIM intCounter
intCounter = 1
Response.write intCounter & " "
intCounter = intCounter + 1
Loop while (intCounter <= 5)


The FOR...NEXT loop is slightly coded differently than DO...WHILE loop. The syntax for the FOR...NEXT loop is:

FOR counter_variable = start_value TO stop_value STEP step_value

The FOR loop always starts with an initial or starting value. After the keyword TO, the stopping value is listed. The stopping value determines when the loop will stop. Then, after the keyword STEP iteration value is listed. This is the value that determines by what value the loop counter variable is updated. Note that the default step value is 1, which means the loop will iterate by 1 if step and the step_value are omitted. Then, the statements that need to be executed inside the loop are listed after the FOR loop statement and before the LOOP keyword. And, the keyword NEXT continues the loop to the next iteration. The statements inside a loop is referred to as loop body. The following code prints numbers from 1 through 5:

for intCounter = 1 to 5
Response.write intCounter & " "

So how and by how much is our intCounter variable updated above, as the loop executes? Again, if you don't use the STEP keyword to specify the update value, the default update value is 1. So our loop will execute five times, printing 1 2 3 4 5 .

In our next example, we print add and even numbers between -5 and 100:

FOR intCounter = -5 to 100 STEP 10
IF intCounter MOD 2 = 0 then
Response.write intCounter & " is an even number" & "<br>"
Response.write intCounter & " is an odd number" & "<br>"
intCounter = intCounter + 1 NEXT

In this example, we set intCounter to a negative number, -5. We iterate intCounter to 100 by 10, each time the loop executes. On line 2, we check if the value of intCounter is even by using the MOD operator. If it is, the statement response.write intCounter & " is an even number" & "<br>" is executed to print current value of intCounter. If the MOD operator determines that the current value of intCounter is an odd number, we execute the response.write statement inside the ELSE clause. The line intCounter = intCounter + 1 makes sure that each time the loop execute we add 1 to intCounter. Thus the value of intCounter is iterated by 11 (10 + 1) each time the loop executes.

Assuming we make no other change to this loop except we delete the last line (intCounter = intCounter + 1), our loop will print odd numbers (-5, 5, 15, 25, etc.). To avoid printing just odd numbers, we add 1 to our intCounter (resulting in -5, 6, 17, 28, etc.). See the output below:

-5 is an odd number
6 is an even number
17 is an odd number
28 is an even number
39 is an odd number
50 is an even number
61 is an odd number
72 is an even number
83 is an odd number
94 is an even number

Infinite loops

If the condition of the loop is not ever met, the loop executes indefinitely. This is why the loop must be properly initialized and iterated to reach the terminating condition value. The following shows an example of an infinite loop:

FOR intCounter = 1 TO 10 STEP -2
response.write intCounter & " is <br>"

In this example intCounter is set to 1 and is being subtracted by -2 each time the loop is executed. Thus condition will never be satisfied and the loop will iterate indefinitely.

Nesting Loops

Loops can be nested; meaning one or more loops can be placed inside of another loop. Assuming that your nested loops are constructed properly, the loops will execute in the order they are listed. Assume we have one loop inside of another. The execution of the loop will start with the outer loop; next the inner loop will be executed from the starting value to the finish value. Then, the outer loop will execute again if the outer loop stop value is not yet reached. The inner loop will execute again from start to finish. In other words, each time the outer loop executes once, the inner loop executes completely from the starting to the finish value. The following example uses two FOR loops to print a multiplication table:

dim strOut
strOut = "<table width=""100"" border=""1"" cellpadding=""2"">"
for row = 1 to 10
strOut = strOut & "<tr align=""center"">"
for col = 1 to 10
strOut = strOut & "<td>" & row * col & "</td>"
strOut = strOut & "</tr>"
strOut = strOut & "</table>"
response.write strOut

To properly align our multiplication table, we use HTML table. In the above code, our first FOR loop (or the outer loop), creates the rows for our table. The second FOR loop creates each column for our row. On line 6 of our code, we multiply the value of row with the value of column and assign the result to the variable strOut. The first time the outer loop executes, it executes with the value 1. The inner loop also start with 1 and goes up to 10. Thus, each time the inner loop executes 1 (the value of the variable row) is multiplied to the value of column (which starts with 1 and goes to 10); resulting in:

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 10 = 10

The following shows the output after the first loop has executed once the second loop has executed completely:


Similarly, remaining table is constructed as the outer loop moves across and the inner loop move down resulting in: