Lesson 5: Loops

In VBScript it is possible to manage the execution of scripts with different control structures. In this lesson, we will look at loops. Loops can be used to repeat parts of a script a specified number of times or until a certain condition is met.

For ... Next

The syntax for a DocumentationFor ...Next loop is:


	For Initializion To Expressions

		 Statement
	Next
	
	

The statement is repeated 'expression' minus 'initialization' number of times. If it doesn't make sense, look at this example:


	<html>
	<head>
	<title>Loops</title>

	</head>
	<body>

	<%
	Dim t

	For t = 1 to 50
	   Response.Write "<p>This text is repeated 50 times</p>"

	Next
	%>

	</body>

	</html>
	
	

In the example, a variable named "t" is used. As you can see, we start by writing Dim t, this is called to declare a variable. You do not need to declare your variables, but it's considered to be good code practice, and it makes your code more clear and easier to understand if someone else at a later time has to read through your code.

Otherwise the example is almost self explanatory. The loop asks the server to repeat the text while t is between 1 and 50. Each time the loop reaches "Next" the variable t will be increased by 1.

It is possible to specify what value that the variable t should be increased by in each loop. This is done with Step as shown in the following example:


	<html>
	<head>
	<title>Loops</title>

	</head>
	<body>

	<%
	Dim t

	For t = 1 to 50 Step 5
	   Response.Write "<p>variable t is now = " & t & "</p>"

	Next

	%>

	</body>
	</html>
	
	

In the example above, t is growing with the value 5 in each loop. Also note how the value t is used as part of the sentence.

Do ... Loop

Another way to make a loop is with DocumentationDo ...Loop on the form:


	Do {While | Until} condition
	   Statement
	Loop 

	
	

Or you can set the condition after Loop instead (no practical difference):

	Do
	   Statement
	Loop {While | Until} condition
	
	

Regardless of whether you use While or Until, the syntax is almost normal English:

Until
Execute until a condition is met.
While
Execute while a condition is met.

Let's look at a simple example:


	<html>
	<head>

	<title>Loops </title>
	</head>
	<body>

	<%
	Dim t

	t = 1

	Do Until t = 6

	   Response.Write "<h" & t & ">Heading Levels</h" & t & ">"

	   t = t + 1

	Loop
	%>

	</body>
	</html>
	
	

Did you catch it? First we declare the variable t and set the value to 1. Then in each loop, we write a heading at level t (h1, h2, h3, etc.) until t is equal to six.

Loops within loops

In principle, there are no limitations on how loops can be used. For instance, you can easily put loops inside loops and thereby create many repeats.

But be careful! ASP becomes slower the more complicated and extensive the scripts. For instance, look at the next example where, with three loops, we can write over 16 million colors!

In order not to make the page slow, we have drastically reduced the number by putting the step to 30 and thereby limited the number of colors to 512.


	<html>

	<head>
	<title>Loops </title>
	</head>
	<body>

	<%
	Dim intRed, intGreen, intBlue, strColor

	For intRed = 0 to 255 Step 30

	   For intGreen = 0 to 255 Step 30
	      For intBlue = 0 to 255 Step 30

		  strColor = "rgb (" & intRed & "" & intGreen & "" & intBlue & ")"

		  
		  Response.Write "<span style='color:" & strColor & "'>" & strColor & " </span>"

	      Next
	   Next
	Next
	%>

	</body>
	</html>
	
	

In the example, each of three primary colors (red, green and blue) can have a value between 0 and 255. Any combination of the three colors creates a color on the form rgb(255,255,255). The color code is used as color in a <span>.

Loops becomes more useful when you've learned a little more. When you understand the principle in a loop, you can proceed to the next lesson, where we look at conditions.



<< Lesson 4: Working with time and dates

Lesson 6: Conditions >>