Free Webmaster Help Logo
Tutorials Articles Tools Images Directory Ask Newsletter
*
 

Tutorials
Cookies
ASP
Advanced HTML
Frames & Tables
.htaccess
Rounded Table Corners
Flash 5
FTP
Beginners
Javascript
PHP/MySQL
Site Promotion
Mobile Internet: WML/WAP
Server Side Includes (SSI)
HTML - The Basics
XHTML
Stylesheets
XML
PHP
*

125x125

Related Links
More ASP Sites
*

 
Sitemap | Contact | Link To Us | Advertise
Report A Problem
Home : Tutorials : ASP : Part 4

ASP Tutorial
Part 4 - Loops and Arrays

Introduction

So far you have learned how to output text, use variables and execute conditional statements. Another very powerful feature of ASP is it's looping funciton.

FOR and NEXT Loops

FOR/NEXT loops are used when you want to execute a piece of code a set number of times. If, for example, you want to output the world 'Hello' 10 times, you could either code it manually or you could use:

<%
For index = 1 to 10
Response.Write("Hello")
Next
%>

Basically, this code says:

For index = 1 to 10

Repeat the following code until the variable 'index' is equal to 10, starting at 1 and going up 1 by 1.

Next

This tells the server to return to the beginning of the loop and increment the variable.

Using The Variable

A loop isn't much use if it just does the same thing over and over again. It really offers no benefits over a simple piece of code. The real power appears when you use the counter variable in your code. If, for example, I wanted to output the numbers 1 to 10 I could use:

<%
For index = 1 to 10
Response.Write(index)
Next
&>


STEP

Step is an extra part you can add on to the end of the For line of the code to change the way it counts. In the loop above, the code starts by setting index to 1, then when Next is reached it adds another 1 (2), the next time it adds another 1 (3) and so on. Using, STEP you can change this action. For example:

<%
For index = 1 to 10 STEP 2
Response.Write(index)
Next
%>

Would output:

246810

It is counting up in 2s. You can also count down:

For index 10 to 1 STEP -1

which will count down from 10 to 1.

While Loops

Another type of loop which can be used in ASP is the While loop. A While loop is written as:

<%
Do While thenumber<10
Resonse.Write("Less than 10")
thenumber = thenumber + 1
Loop
%>

To explain this code:

Do While thenumber<10

This code first checks if the variable thenumber has a value which is less than 10, then if it is executes the following code until it reaches:

Loop

This tells the code to return to the Do line. Now, you may have noticed the problem here. If all the Do line does is check whether thenumber has the value of less than 10, the loop will go on forever. This is why the line:

thenumber = thenumber + 1

has to be included. This increments the value of thenumber, so that it will eventually be more than 10, and the loop will end. Of course, you aren't just limited to adding and subtracting as you are with a For loop. You can make any changes to the variable you like in the code.

Until Loops

A third type of loop is the Until loop. This is almost exactly the same as the While loop:

<%
Do Until thenumber=10
Response.Write("Less than 10")
thenumber = thenumber + 1
Loop
%>

The difference between this and a While loop is that the code will execute until the conditionin the Do line is met, unlike a While loop where it will only execute while the condition is met. As with the While loop you must increment the variable yourself.

Part 5

In part 5 I will show you how to use ASP with forms to take input from the user.

Note: This tutorial is currently being developed and further parts will be posted shortly.

© 1999 - 2001 FreeWebmasterHelp.com