
Asp Looping in ASP
Sure! Here's how looping works in Classic ASP (using VBScript):
Looping in ASP (VBScript)
Classic ASP primarily uses VBScript, which supports several loop types:
1. For...Next Loop
<%Dim iFor i = 1 To 5 Response.Write "Iteration: " & i & "<br>"Next%>
Output:
Iteration: 1Iteration: 2Iteration: 3Iteration: 4Iteration: 5
2. For Each...Next Loop
Used to loop through collections or arrays.
Example with an array:
<%Dim fruits, fruitfruits = Array("Apple", "Banana", "Cherry")For Each fruit In fruits Response.Write fruit & "<br>"Next%>
3. Do While Loop
<%Dim countcount = 1Do While count <= 5 Response.Write "Count is " & count & "<br>" count = count + 1Loop%>
4. Do Until Loop
<%Dim countcount = 1Do Until count > 5 Response.Write "Count is " & count & "<br>" count = count + 1Loop%>
5. While...Wend Loop (less commonly used)
<%Dim countcount = 1While count <= 5 Response.Write "Count is " & count & "<br>" count = count + 1Wend%>
Summary:
Loop Type | When to Use |
---|---|
For...Next | Loop a known number of times |
For Each...Next | Loop through all items in a collection or array |
Do While | Loop while condition is True |
Do Until | Loop until condition becomes True |
While...Wend | Loop while condition is True (older syntax) |
If you want, I can help with:
Nested loops examples
Looping through database recordsets
Breaking out of loops (using
Exit For
orExit Do
)Anything else about ASP loops!
Just ask!