Did you find this useful?
Socialize it today.


ASP Looping Statements

Wednesday, August 22, 2012

tags ASP

As with most programming languages, ASP allows (through VBScript) for several types of repetitive operations, also commonly known as loops. Loops are used to repeat the same group of statements until a specified condition is met. In most cases, you control the number of iterations with a variable used as a counter, which typical increments until a certain target number is reached. However, you can also loop through a set of data until you reach the end of the set. The three main types of loops that you can use are For..Next, For Each..Next, and Do Loops. There are other types of loops, such as the While..Wend, but these are the three main ones that you should be using.


The For..Next

For...Next loops are used when you want to execute a block of code a specific number of times. The For statement specifies the counter variable and its start and end values. The Next statement increases the counter variable by one. You can use the optional Step keyword to increase or decrease the counter variable by the value you specify.


<!DOCTYPE html>
<html>
<head>
  <title>My Page</title>
</head>
<body>
<%
For x = 0 To 5
  Response.Write("The number is " & x & "<br />")
Next
%>
</body>
</html>

You can use the Step keyword to increase or decrease the counter variable by the value you specify. This value can be either positive or negative.


<%
For x = 0 To 10 Step 2
  Response.Write("The number is " & x & "<br />")
Next
%>


The For Each..Next Loop

The For Each..Next loop repeats a block of code for each item in a collection, or for each element of an array.


<%
Dim colors(6)
colors(0) = "Red"
colors(1) = "Orange"
colors(2) = "Yellow"
colors(3) = "Green"
colors(4) = "Blue"
colors(5) = "Indigo"
colors(6) = "Violet"
For Each x In colors
  Response.Write(x & "<br />")
Next
%>


The Do Loop

You can use the Do..Loop when you need to repeat a block of code, but you do not know how many repetitions are needed. The statements are repeated either while a condition is TRUE or until a condition becomes TRUE.


<%
Do While x < 10
  Response.Write(x & "<br />")
  x = x + 1
Loop
%>

<%
Do Until x > 10
  Response.Write(x & "<br />")
  x = x + 1
Loop
%>


The Exit Keyword

You can also always exit a loop block by using the Exit keyword. The Exit keyword simply alters the flow of control by causing an exit from a repetitive cycle. You can use the Exit keyword in various situations, such as when avoiding for an endless loop.


<%
Do While x < 10
  Response.Write(x & "<br />")
  If x = 5 Then Exit Do
  x = x + 1
Loop
%>


Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

ASP In A Nutshell Sams Teach Yourself Active Server Pages 3.0 in 21 Days Beginning ASP Databases