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…
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.
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 :
Output of Example 1.0 :
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 :
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 ) Do Until Conditions Become True
Until keyword repeats operations until the conditions becomes true.
Structure 1.2 :
Structure 1.3 :
Let’s make some example to learn how to use until keyword.
Example 1.2 : Generating a random security number
Probable Output of Example 1.2 :
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 :
Probable Output of Example 1.3 :
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 :
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.
Output of Example 1.5 :
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:
We could make the Example 1.0 with While … Wend Statement. Look at the Example 2.0 :
Example 2.0 :
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 :
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 :
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 :
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 :
Output of Example 3.1
As you see, 5 is added to i each time it loops.
Example 3.2 :
Output of Example 3.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 :
Output of Example 3.3
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
Example 4.0
Output of Example 4.0
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”
Output of Example 4.1
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.

Recently Typed