Classic ASP โ Looping โ Use For, While, and Do Loops in VBScript
Introduction โ What Is Looping in Classic ASP?
Looping in Classic ASP lets you repeat blocks of code using VBScript until a condition is met. It’s essential for iterating through arrays, database records, or repeating HTML elements like lists and tables dynamically.
In this guide, youโll learn:
- How to use
For,For Each,While, andDo...Loop - Syntax rules and examples
- When to use each type of loop
- Common mistakes and best practices
For...Next Loop
Use when you know the number of iterations in advance.
<%
Dim i
For i = 1 To 5
Response.Write "Item " & i & "<br>"
Next
%>
Output:
Item 1
Item 2
Item 3
Item 4
Item 5
For Each...Next Loop
Ideal for iterating over arrays or collections.
<%
Dim colors
colors = Array("Red", "Green", "Blue")
Dim color
For Each color In colors
Response.Write color & "<br>"
Next
%>
Output:
Red
Green
Blue
Do While...Loop
Executes the loop as long as the condition is true.
<%
Dim i
i = 1
Do While i <= 3
Response.Write "Count: " & i & "<br>"
i = i + 1
Loop
%>
Output:
Count: 1
Count: 2
Count: 3
Do...Loop While
Checks condition after running once. Guaranteed to execute at least once.
<%
Dim i
i = 1
Do
Response.Write "Value: " & i & "<br>"
i = i + 1
Loop While i <= 2
%>
Output:
Value: 1
Value: 2
Example โ Loop with Conditional Logic
<%
Dim scores
scores = Array(92, 75, 84)
Dim score
For Each score In scores
If score >= 90 Then
Response.Write "A (" & score & ")<br>"
ElseIf score >= 80 Then
Response.Write "B (" & score & ")<br>"
Else
Response.Write "C (" & score & ")<br>"
End If
Next
%>
Output:
A (92)
C (75)
B (84)
Common Mistakes in ASP Looping
| Mistake | Correction |
|---|---|
| Infinite loop (no exit condition) | Always update your counter inside loop |
Missing Next, Loop, or Wend | Close all loops properly |
| Looping past array bounds | Use UBound() to limit array length |
Looping Over Form Values
<%
Dim field
For Each field In Request.Form
Response.Write field & ": " & Request.Form(field) & "<br>"
Next
%>
Output:
Displays all POST form fields and their values.
Best Practices for Classic ASP Loops
Do:
- Use
For Eachfor cleaner array iteration - Keep loop logic concise and well-indented
- Include exit logic to avoid infinite loops
Avoid:
- Mixing loops with heavy business logic
- Forgetting to increment counters
- Modifying the looped collection during iteration
Summary โ Recap & Next Steps
Looping is a core part of Classic ASP, allowing you to dynamically repeat output or logic. Mastering For, For Each, and Do While lets you build smarter, more efficient scripts.
Key Takeaways:
- Use
For...Nextwhen count is known - Use
For Eachfor arrays/collections - Use
Do WhileandDo Untilfor flexible looping
Real-world Use Cases:
- Rendering lists or tables from arrays
- Looping through form fields or query strings
- Pagination, validation, and totals calculation
FAQs โ Classic ASP Looping
Can I break out of a loop in VBScript?
Yes, use Exit For or Exit Do to break the loop manually.
Whatโs the difference between While...Wend and Do While...Loop?
Both work, but Do While is more flexible and preferred in Classic ASP.
How do I loop through a database recordset?
Use Do Until rs.EOF with rs.MoveNext inside the loop.
Share Now :
