Home > ASP > ASP Loops

ASP Loops

We are going to talk about doing loops with ASP. There are many different methods to make a loop with ASP and we should choose the best way. There is a list of all looping statements in asp:

I. Do … Loop Statement
II. While … Wend Statement
III. For … Next Statement
IV. For Each … Next Statement

I. Do … Loop Statement

This statement can be very useful if you don’t have or don’t know number of repetitions because it loops the operations until or while specified conditions are true.

1.1 ) Do While Conditions Are True

The While keyword repeats operations while conditions are true. We make our operations by using the while keyword after do or after loop statements. Every do statement have to be closed with loop statement.

Structure 1.0 : If we use while keyword near do statement, the condition will be evaluated before the loop begin. If conditions are false then loop will be skipped else operations are executed then the conditions will be evaluated again. If conditions are still true then it will execute the operations again…

1
2
3
4
5
6
7
<%
do while ( conditions )
'...
'operations
'...
loop
%>

Structure 1.1 : There is a little but important difference between Structure 1.0 and Structure 1.1. In Structure 1.1, conditions are tested at the end of the loop. The loop is always executed at least once with this usage.

1
2
3
4
5
6
7
<%
do
'...
'operations
'...
loop while ( conditions )
%>

There is a simple code example that shows us how to use do … loop statements with while keyword in asp. We can see with this usage how to give numbers to elements of a list.

Example 1.0 :

1
2
3
4
5
6
7
8
<%
my_array=Array("Michael","Vincent","Jack","John","Desmond")
i=0
do while i=<4
Response.Write "Element "&(i+1)&" = "&my_array(i)&"<br>"
i=i+1
Loop
%>

Output of Example 1.0 :

1
2
3
4
5
Element 1 = Michael
Element 2 = Vincent
Element 3 = Jack
Element 4 = John
Element 5 = Desmond


1.1.1 ) Using Do … Loop Statement and WHILE Keyword with Database

If we want to make a sample list of our database records, do … loop can be used, but we advice until keyword (that we will talk about it a few lines after) instead of while keyword.

Example 1.1 :

1
2
3
4
5
6
7
8
9
10
<%
Set MyRsName = Server.CreateObject("ADODB.RecordSet")
SQL = "Select person_name from persons order by person_name"
MyRsName.Open SQL,my_conn,1,3

do while not MyRsName.Eof
Response.Write MyRsName("person_name")&"<br>"
MyRsName.MoveNext
Loop
%>

We tried to make a list of all records in persons table with Example 1.1. This script will write every single person’s name in a single line.

Output of Example 1.1 :

1
2
3
4
5
6
Anthony
George
Michael
Slevin
Smith
Sucre


1.2 ) Do Until Conditions Become True

Until keyword repeats operations until the conditions becomes true.

Structure 1.2 :

1
2
3
4
5
6
7
<%
do until ( conditions )
'…
'operations
'…
loop
%>

Structure 1.3 :

1
2
3
4
5
6
7
<%
do
'…
'operations
'…
loop until ( conditions )
%>

Let’s make some example to learn how to use until keyword.

Example 1.2 : Generating a random security number

1
2
3
4
5
6
<%
randomize
do
my_security_code = int(rnd*99999)
loop until my_security_code > 10000
%>

Probable Output of Example 1.2 :

1
94153

We generated a security code that bigger than 10000. The structure 1.3 is used to make this example because the loop should be executed at least once to generate a random security code. The same application could be made like this:

Example 1.3 :

1
2
3
4
5
6
7
8
9
<%
randomize
my_security_code = int(rnd*99999)
'or we can specify the variable’s value manually like this:
'my_security_code=1
do until my_security_code > 10000
my_security_code = int(rnd*99999)
loop
%>

Probable Output of Example 1.3 :

1
35149


1.2.1 ) Using Do … Loop Statement and UNTIL Keyword with Database

If we want to make a sample list of our database records, until keyword is more practical than while keyword. Let’s do the Example 1.1 again with until keyword:

Example 1.4 :

1
2
3
4
5
6
7
8
9
10
<%
Set MyRsName = Server.CreateObject("ADODB.RecordSet")
SQL = "Select person_name from persons order by person_name"
MyRsName.Open SQL,my_conn,1,3

do until MyRsName.Eof
Response.Write MyRsName("person_name")&"<br>"
MyRsName.MoveNext
Loop
%>

As you see, it is not necessary to write not keyword before the this condition and operations going to be executed until condition become true.

1.3 ) If These Conditions Become True Then Exit Do

Sometimes, we need to break the loop. So, for breaking the loop we use the exit do keyword.

Example 1.5 : Exiting from do… loop statement.

1
2
3
4
5
6
7
8
9
10
11
<%
Set MyRsName = Server.CreateObject("ADODB.RecordSet")
SQL = "Select person_name from persons order by person_name"
MyRsName.Open SQL,my_conn,1,3

do until MyRsName.Eof
if MyRsName("person_name")=”Slevin” then exit do
Response.Write MyRsName("person_name")&"<br>"
MyRsName.MoveNext
Loop
%>

Output of Example 1.5 :

1
2
3
Anthony
George
Michael

In the Example 1.5, it breaks the loop if the person is Slevin.

II. While … Wend Statement

While … wend statement is less functional than do … loop statement. In addition, while…wend method can be used as same as do while … loop method.

Structure 2.0:

1
2
3
4
5
6
7
<%
while ( conditions )
'…
'operations
'…
wend
%>

We could make the Example 1.0 with While … Wend Statement. Look at the Example 2.0 :

Example 2.0 :

1
2
3
4
5
6
7
8
<%
my_array=Array("Michael","Vincent","Jack","John","Desmond")
i=0
while i=<4
Response.Write "Element "&(i+1)&" = "&my_array(i)&"<br>"
i=i+1
Wend
%>

III. For … Next Statement

The for … next statement depends on value of a variable ( that increases or decreases by the same amount each time through the loop ) and loops the operations until the variable reaches a specified value. This variable could be called counter.

Structure 3.0 :

1
2
3
4
5
6
7
<%
for VARIABLE=VALUE to SPECIFIED_VALUE
'…
'operations
'…
next
%>

For … next statement can be more practical than the other loop statements. For instance, this statement would be the best choice if we want to make Example 1.0.

Example 3.0 :

1
2
3
4
5
6
<%
my_array=Array("Michael","Vincent","Jack","John","Desmond")
for i=0 to 4
Response.Write "Element "&(i+1)&" = "&my_array(i)&"<br>"
Next
%>


3.1 ) Using Step Keyword with For … Next Statement

Sometimes, we need to increase or decrease value of our variables by a specified amount.

Structure 3.1 :

1
2
3
4
5
6
7
<%
for VARIABLE=VALUE to SPECIFIED_VALUE step AMOUNT
'…
'operations
'…
next
%>

When you determine an AMOUT, the AMOUNT will be added to VALUE of VARIABLE. Look at the Example 3.1 to make clear this:

Example 3.1 :

1
2
3
4
5
<%
for i=1 to 40 step 5
Response.Write "Number = "&i&"<br>"
Next
%>

Output of Example 3.1

1
2
3
4
5
6
7
8
Number = 1
Number = 6
Number = 11
Number = 16
Number = 21
Number = 26
Number = 31
Number = 36

As you see, 5 is added to i each time it loops.

Example 3.2 :

1
2
3
4
5
<%
for i=20 to 1 step -3
Response.Write "Number = "&i&"<br>"
Next
%>

Output of Example 3.2

1
2
3
4
5
6
7
Number = 20
Number = 17
Number = 14
Number = 11
Number = 8
Number = 5
Number = 2


3.2 ) Exiting From a For … Next Loop

If you need to exit from the loop before the counter reaches its specified end value, you can use the exit for keyword. Let’s make the Example 3.1 again but this time we have a condition.

Example 3.3 :

1
2
3
4
5
6
<%
for i=1 to 40 step 5
if i>20 then exit for
Response.Write "Number = "&i&"<br>"
Next
%>

Output of Example 3.3

1
2
3
4
Number = 1
Number = 6
Number = 11
Number = 16

As it seems, when the condition became true (value of i bigger than 20), the loop is broken.

IV. For Each … Next Statement

For each … next statement is used for executing a block of operations for each element of a collection. There is no counter in for each … next loop but you have to declare an object variable.

Structure 4.0

1
2
3
4
5
6
7
<%
for each ELEMENT in COLLECTION
'…
'operations
'…
next
%>

Example 4.0

1
2
3
4
5
6
<%
my_array=Array("Michael","Lincoln","Mahone","Sucre","T-Bag")
for each x in my_array
Response.Write x&"<br>"
Next
%>

Output of Example 4.0

1
2
3
4
5
Michael
Lincoln
Mahone
Sucre
T-Bag

We can use for each … next statement for request variables. For example, let’s get the all querystrings in this following url:

Example 4.1
page_url = “http://www.memiso.com/example4_1.asp?q=This%20is&v=an&s=example

1
2
3
4
5
<%
for each item in Request.Querystring
Response.Write item&": "&Request.Querystring(item)&"<br>"
Next
%>

Output of Example 4.1

1
2
3
q : This is
v : an
s : example

We can break the loop via exit for keyword as we did with for … next statement.

Conclusion

To sum up, there are four looping statements in ASP. Do … loop and for … next statements are the most functional statements. Since you have these both, you can do everything that you can imagine.

We tried to introduce them simply. However, if you are in trouble with asp loops you can leave a comment and ask your questions. We will be glad to help you.

Bookmark and Share
Categories: ASP Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.
eXTReMe Tracker