Article
ASP Language Basics
Loops
The rest of the control structures that I'll discuss in this article have to do with making your script perform the same operation several times without having to type the commands for that operation over and over again. These structures are normally called loops.
The first type of loop is called a Do-While loop. Its name suits its function: to do something over and over again while a condition remains true. Here's what a Do-While loop looks like:
Do While condition
' a block of commands (the loop body)
Loop
The operation of a Do-While loop is best illustrated by the flowchart in Figure 1.

In most cases, the condition for a Do-While loop will be something that starts off being true, but eventually will change to false after the block of commands has been executed a certain number of times. Here's a simple script that uses a Do-While loop:
1 <% Option Explicit %>
2 <!-- Countdown.asp
3 A simple ASP script that counts down from 10. -->
4 <html>
5 <head>
6 <title> Countdown to Liftoff </title>
7 </head>
8 <body>
10 <%
11 Dim intCount
12 intCount = 10
13
14 Response.Write "<p>Countdown to liftoff... T Minus "
15
16 Do While intCount > 0
17 Response.Write intCount & "!<br>"
18 intCount = intCount - 1
19 Loop
20
21 Response.Write "Liftoff! We have liftoff!</p>"
22 %>
23 </body>
24 </html>
Here's what's new in this program:
- Lines 11-12: We declare a variable called
intCountwith an initial value of 10. - Line 16: A
Do-Whileloop that will continue for as long asintCountis greater than 0. - Line 18: Subtract 1 from the value stored in
intCount
So when the program first gets to the Do-While loop, intCount is 10 so the loop body is executed, printing out "10!" and then subtracting 1 from the value stored in intCount. The condition is then checked again. 9 is still greater than zero, so the loop body is executed once more ("9!"). This continues until the 10th time the loop body is executed ("1!"), when subtracting 1 from intCount brings its value to zero. Now when the condition is checked it is false; thus, the loop stops and execution continues following the loop.
Here's what you should see when you view the results of the script in your browser:
Countdown to liftoff... T Minus 10!
9!
8!
7!
6!
5!
4!
3!
2!
1!
Liftoff! We have liftoff!
Do-While loops are very handy, but there are two limiting cases you should be aware of:
- If the condition is false to begin with, the loop body won't be executed at all.
- If the condition stays true forever, the script will execute the loop over and over and will eventually time out, causing an error message to be displayed.
While the first case has its uses, the second case is something to be careful of. A loop that never ends is called an infinite loop. Infinite loops can happen under the most unexpected circumstances, and are a surprisingly common mistake that even experienced programmers sometimes make. If one of your scripts ever seems to get 'stuck' and just leaves your browser hanging, chances are good that it's gotten stuck in an infinite loop. If you wait long enough, eventually the script will run into the script time limit on your server and an error message explaining that the script exceeded the maximum allowable execution time. To see what this would look like, just change the condition for the Do-While loop on line 16:
16 Do While True
Don't worry; it won't hurt your computer. It might slow things down a bit until the script times out, though.