Looping the Loop
This tutorial shows you how to make stuff happen repeatedly using VBScript's looping commands.
In this tutorial we will learn how to execute a piece of code more than once. In real life we often do something repeatedly - wake up, shower, go to work, go to bed, wake up ... for example. As with decision making, repeating an action is something that we often want to do in programming. VBScript provides us with several statements that allow us to repeat actions. We'll go through them one by one.
Counting from 1 to 10
One of the things we often want to do, is to repeat a piece of code an exact number of times. The easiest way to do this is using the
For loop. To use another real-world example, from age 5 to 16, we generally go to school. We could write this using a
For loop as follows:
<% For i = 5 To 16 Response.Write "You are " & i & "." Response.Write "Go to school.<br>" Next Response.Write "School's out!" %>
This would produce output like this:
You are 5. Go to school. You are 6. Go to school. ... You are 15. Go to school. You are 16. Go to school. School's out!
For statement uses a variable to keep track of repetitions. In this example,
i is set to 5 initially. The computer executes all the statements between the
For line and the
Next line each time it goes through the loop. When it gets to the end, it goes back to the
For line and adds 1 to the variable. It continues in this way until the variable reaches the end value - in this case 16. Execution then continues with the line after the
How about if you want to count only even numbers, or count backwards? To do this, you can use the optional
Step part of the
For loop. This tells the processor what to increment the variable by each time through the loop. So, to jump in steps of two, you would write:
<% For i = 2 To 10 Step 2 ... Next %>
and to go backwards, you would write:
<% For i = 10 To 1 Step -1 ... Next %>
While the Sun Shines
Another commonly used loop is the
While loop. In this loop, the processor continues to exit the code while a condition is true. If you're not familiar with conditions, you might want to read the Making your mind up tutorial before you carry on. Here's an example:
<% While Not SchoolHolidays Response.Write "Go to school.<br>" If TheDay = "Saturday" Or TheDay = "Sunday" Then SchoolHolidays = True End If Wend %>
SchoolHolidays is what's known as a boolean variable. A boolean variable can be set to either True or False.
When the processor reaches the
While statement it will evaluate the condition
Not SchoolHolidays to find out whether it's true or false. If it's True, the processor will execute the code within the loop until it reaches the
Wend statement. At that point, it will jump back to the
While statement and evaluate the condition again. If it's False this time, the processor will jump down to the line of code that follows the
Doing the Do
Do loop operates in more than one way, and so is more flexible than the
While loop. We can re-write our example from above using the
Do loop like this:
<% Do While Not SchoolHolidays Response.Write "Go to school.<br>" If TheDay = "Saturday" Or TheDay = "Sunday" Then SchoolHolidays = True End If Loop %>
The example above will execute in an identical way to the
While loop from further up the page. A related example follows. Can you work out what the subtle difference is?
<% Do Response.Write "Go to school.<br>" If TheDay = "Saturday" Or TheDay = "Sunday" Then SchoolHolidays = True End If Loop While Not SchoolHolidays %>
In the first example, the condition is evaluated at the top of the loop, while the second example evaluates the condition at the bottom of the loop. Well done if you worked it out! What this means in practise is that the
While ... Wend and
Do While ... Loop examples will both execute the statements within the loop zero or more times. In the
Do ... Loop While example the loop will be executed one or more times.
While the loop is executed while the condition is true.
Do can also be used with
Until to repeat a loop until a condition becomes true. This is our example written using
Do Until ... Loop:
<% Do Until SchoolHolidays Response.Write "Go to school.<br>" If TheDay = "Saturday" Or TheDay = "Sunday" Then SchoolHolidays = True End If Loop %>
Finally, the same example can be written in this form (remember we go through the loop at least once when the condition is at the end of the loop):
<% Do Response.Write "Go to school.<br>" If TheDay = "Saturday" Or TheDay = "Sunday" Then SchoolHolidays = True End If Loop Until SchoolHolidays %>
When you're deciding which loop to use, first work out whether you need to execute a loop a specific number of times - the
For loop is the most appropriate for this situation. If you don't know how many repetitions you'll need then determine whether you need to execute the loop at least once - for this situation,
Do ... Loop condition is appropriate. Otherwise use either
Do condition ... Loop or
While ... Wend according to which statement you prefer.
Now it's your turn
To test your new found skills, here's a little exercise for you to try:
Write an ASP page to output the square of the numbers 1 to 10 using different kinds of loops. Your output should look something like this:
12 = 1 22 = 4 32 = 9 ... 102 = 100
Start off by using a
For ... Next loop to generate the output. When you've got that working, change your code to use the
While ... Wend loop, and finally try using one or both of the
Do loops. Which loop was the most appropriate for this exercise?
Responses to this article
There are no responses yet.
Post a response
Want to add a comment, or ask a question about this article? Post a response.
To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.