Home
Link To Us
Components
Resources










Site Menu

An Overview in Displaying Database Information on the Web

Setting up a DSN-Less Database Connection

The Complete FileSystem Object Reference Guide

Search the Web for other ASP Tutorials

A Good ASP Database Resource
cover
More Resources




















Top Ranked
Reference Books









Loop-the-Loop

Active Server Pages contains various loops that can be used to process data. These loops include:

If-Then-Else Loop
For-Next Loop
For-Each-Next Loop
Do-While/Until Loop
While-Wend Loop

We will now discuss each of these loop types individually.

If-Then-Else Loop

This loop allows for checking of code, if a statement is found then certain processing occurs, otherwise something else is processed.


If name="Joe" then
	Response.write("Hello Joe!")
else
	Response.write("I don't think we have met.")
End If

In this example if the variable "name" is equal to the value "Joe" then the "Hello Joe!" string is outputted. Otherwise, the script will output "I don't think we have met."

For-Next Loop

The For-Next loop allows for code to be processed until a specific goal is reached. We have already seen one example of the for-next loop. Here is another example:


for i=1 to 20
	response.write(i)
next

This example outputs the value of "i" 20 times, each time incrementing it once. This would produce:1234567891011121314151617181920. This allows for code to be run a multiple of times very easily. At any time in a For-Next Loop the loop can exit using an "Exit For" statement.


y=7
for i=1 to 20
	response.write(i)
	if i=y then exit for
next

In this example when "i" equals "y" the For-Next Loop will not continue and the rest of the page will process.

For-Each-Next Loop

The For-Each-Next Loop is specifically designed for arrays, collections, or dictionaries. This allows for processing to occur on each value in one of these types of situations. If you had an array, for example, you could process for each value in the array.


Dim x(20)
For Each g in x
	response.write(g)
Next

The "Exit For" statement works the same way in this situation as it does in the previous For-Next Loop.

Do-While/Until Loop

The Do-While/Until Loop allows for bits of code to be run Until a goal is attained or While a goal is not attended.


x=1
Do While x<10
	Response.write("Hi There!<BR>")
	x=x+1
Loop

This outputs Hi There!<BR> to the page each time incrementing "x". Once x has reached the value of "10" the loop is broken and the page continues processing. Do-Until loops basically work the same way, but instead of proceding through the loop while a value remains true, it procedures through the loop until something becomes true.

At any time in a Do-While/Until Loop the loop can exit using an "Exit Do" statement.


x=10
y=7
Do Until x<5
	response.write("hello")
	x=x-1
	if x=y then Exit Do
Loop

In this example when "x" equals "y" the loop will not continue and the rest of the page will process.

While-Wend Loop

The final loop type we will discuss is the While-Wend. The While-Wend loop will process given statements until a goal or condition is achieved. This type of loop is very similar to the Do-While Loop and generally not thought of as structured as the Do-While Loop.


x=1
While x<10
	Response.write("Hi There!<BR>")
	x=x+1
Wend

This outputs Hi There!<BR> and increments "x". Once x has reached the value of "10" the While-Wend loop is broken and the page continues processing.


Sections

Section 1: ASPease
Section 2: Loop-the-Loop
Section 3: Make a Statement
Section 4: Final Results

The Bookworm
Search the Tutorial-Web library for books on any topic!
Sponsors

Search Now:
In Association with Amazon.com

Enter your email address to join our mailing list!
We will NOT sell your name to advertisers