Article
ASP Language Basics
For Loops
The final control structure we'll be seeing in this article is similar to a Do-While loop, and is called a For loop. The For loop provides a nice shortcut for the most common application of Do-While loops. Here's what the syntax looks like:
For counter = start To end [Step step]
' loop body
Next
In the above, counter is the name of a variable that will store a value that will count either up or down each time through the loop, start is the value that the counter variable should start at the first time through the loop, and end is the value that the counter variable should have the last time through the loop. The optional step parameter is the amount by which the counter variable should change after each time through the loop. If it is not specified, the value will simply be increased by one each time through the loop. The operation of a For loop is illustrated in Figure 2.

To demonstrate a For loop in practice, I've adapted the countdown script above to use a For loop instead of a Do-While loop. Note that in this case we want the counter variable to count down from 10 to 1, so we specify a step value of –1.
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>
9 <%
10 Dim intCount
11
12 Response.Write "<p>Countdown to liftoff... T Minus "
13
14 For intCount = 10 To 1 Step -1
15 Response.Write intCount & "!<br>"
16 Next
17
18 Response.Write "Liftoff! We have liftoff!</p>"
19 %>
20 </body>
21 </html>
The code in this script is fairly similar to the original version with the Do-While loop; we've just cut a few corners! The creation of the intCount variable stayed the same (line 10), but we no longer need to give it an initial value, because the For loop takes care of that. The command from the loop body that subtracted 1 from the value of the intCount variable is no longer necessary either. So, what a For loop lets you do is combine all of the commands having to do with the initialization, checking, and updating of a counter variable (intCount, in this case) into a single line, making the code in general easier to read.
Even though any For loop could be expanded out into a Do-While loop with the appropriate statements before the loop and at the end of the loop body, it is common practice (and generally tidier) to express any loop that uses a counter variable as a For loop.
Summary and Further Reading
Learning the basic syntax of a programming language is rarely very riveting stuff, so unfortunately such material doesn’t make for the most exciting of articles. Nevertheless, in this article you have been exposed to the most important features of the VBScript programming language commonly used for writing dynamic Web sites in ASP. We looked at how variables let you store data for use in calculations, what operators are available for putting pieces of data together and getting a result, and what control structures let you make programs that do something other than go in a straight line.
A few of the less basic features of the language (such as the select-case control structure and logical operators) were left out due to space constraints, but these will be covered where appropriate in later instalments of the series. In the meantime, if you’re looking for a complete guide to the VBScript language, check out the Microsoft Scripting Technologies Web site.
With the mundane details of VBScript out of the way, you should now be ready to make the leap into the world of building dynamic Web sites with ASP. And that’s exactly what I’ll be covering in next month’s article! See you back here in 30!