VKS-LEARNING HUB

Home » Class XII » MMWT-XII » ASP LOOPS

ASP LOOPS

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.

Do While Loop Do Until Loop
do  while check the condition in the beginning
if the condition turns out to be true then the block of statements are executed
In do  Until loop   condition is tested, if the condition turns out to be false the block of  then the statements are executed
 <%

mynumber=0
Do While mynumber<10
response.write(“Hello<HR>”)
mynumber=mynumber+1
Loop
%>

<%
mynumber=0
Do Until mynumber=10
response.write(“Hello<HR>”)
mynumber=mynumber+1
Loop
%>

Leave a comment