Chapter 33: VBScript Looping
VBScript Looping.
This is the exact lesson that comes right after VBScript Conditional Statements in the W3Schools ASP track, and it is absolutely essential because almost every real Classic ASP page needs to repeat something:
- show rows from a database
- list products in a cart
- generate menu items
- display search results
- build HTML tables
- create pagination links
- loop over form fields or cookies
Without loops, your pages would be very static and repetitive to code.
I will explain it like your favorite teacher who actually wrote and maintained Classic ASP sites in the early 2000s — slowly, clearly, with many practical examples, good habits, common mistakes, and real-world patterns you would see in actual .asp files back then (and still in some legacy systems today).
1. The Four Main Loop Types in VBScript (Classic ASP)
VBScript offers four looping constructs — here they are in order of how often you actually used them in real Classic ASP code:
| Loop Type | Keyword Syntax | When you use it most often | Real-world frequency in 2000–2010 code |
|---|---|---|---|
| For Each … Next | For Each item In collection … Next | Looping over arrays, dictionaries, recordsets | ★★★★★ (most common by far) |
| For … Next | For i = start To end [Step n] … Next | When you need a counter / index | ★★★★☆ |
| Do While … Loop | Do While condition … Loop | When you don’t know how many times to loop | ★★★☆☆ |
| Do Until … Loop | Do Until condition … Loop | Similar — loop until something becomes true | ★★☆☆☆ |
Quick rule of thumb (what most Classic ASP developers followed):
- Use For Each → whenever possible (cleanest, safest)
- Use For … Next → when you need the index number
- Use Do While / Do Until → mostly with database recordsets (old ADO style)
2. Example 1 – For Each … Next (The King of Loops)
This is the loop you will see in 80–90 % of real Classic ASP pages that show repeating data.
|
0 1 2 3 4 5 6 |
<%@ Language=VBScript %> <% Option Explicit %> <!DOCTYPE html> <html> <body> <h2>Hyderabad Street Food Favorites</h2> <ul> <% Dim foods foods = Array("Hyderabadi Biryani", "Haleem", "Pathar ka Gosht", "Irani Chai", "Osmania Biscuit", "Double Ka Meetha") Dim food For Each food In foods Response.Write "<li>" & Server.HTMLEncode(food) & "</li>" Next %> </ul> </body> </html> |
Why this is so popular:
- No need to know the size (UBound)
- No risk of off-by-one errors
- Clean and readable
- Works with arrays, collections, Recordsets, Dictionaries
3. Example 2 – For … Next (When You Need Index / Position)
|
0 1 2 3 4 5 6 |
<% Dim products, prices, i products = Array("Biryani", "Haleem", "Kebab", "Qubani ka Meetha") prices = Array(399.00, 450.00, 520.00, 180.00) Response.Write "<table border='1'>" Response.Write "<tr><th>#</th><th>Item</th><th>Price</th></tr>" For i = 0 To UBound(products) Response.Write "<tr>" Response.Write "<td>" & (i + 1) & "</td>" Response.Write "<td>" & Server.HTMLEncode(products(i)) & "</td>" Response.Write "<td>₹" & FormatNumber(prices(i), 2) & "</td>" Response.Write "</tr>" Next Response.Write "</table>" %> |
Key points:
- For i = 0 To UBound(array) — very common pattern
- UBound(array) = highest index (for 0-based array = count – 1)
- Use index i when you need numbering, alternating rows, first/last styling
Alternating row colors (very typical):
|
0 1 2 3 4 5 6 |
For i = 0 To UBound(products) Dim rowClass rowClass = IIf(i Mod 2 = 0, "even", "odd") Response.Write "<tr class='" & rowClass & "'>" ' ... Next |
4. Example 3 – Do While / Do Until (Mostly used with Recordsets)
This is the classic database loop you would see in almost every 2000–2008 ASP site.
|
0 1 2 3 4 5 6 |
<% Dim conn, rs Set conn = Server.CreateObject("ADODB.Connection") conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("northwind.mdb") Set rs = conn.Execute("SELECT ProductName, UnitPrice FROM Products ORDER BY ProductName") If Not rs.EOF Then Response.Write "<table border='1'>" Response.Write "<tr><th>Product</th><th>Price</th></tr>" Do While Not rs.EOF Response.Write "<tr>" Response.Write "<td>" & Server.HTMLEncode(rs("ProductName")) & "</td>" Response.Write "<td>" & FormatCurrency(rs("UnitPrice")) & "</td>" Response.Write "</tr>" rs.MoveNext Loop Response.Write "</table>" Else Response.Write "<p>No products found.</p>" End If rs.Close conn.Close Set rs = Nothing Set conn = Nothing %> |
Do Until version (also very common):
|
0 1 2 3 4 5 6 |
Do Until rs.EOF ' ... same code ... rs.MoveNext Loop |
Why Do While Not rs.EOF was everywhere:
- ADODB Recordset has .EOF property
- You must call .MoveNext manually
- No automatic enumeration like modern languages
5. Quick Reference Table – VBScript Loops in Classic ASP
| Loop Type | Syntax Example | Best for | Frequency in old code |
|---|---|---|---|
| For Each | For Each item In collection … Next | Arrays, Recordsets, Dictionaries | ★★★★★ |
| For … Next | For i = 0 To UBound(arr) … Next | Need index / counter | ★★★★☆ |
| Do While … Loop | Do While Not rs.EOF … rs.MoveNext … Loop | Database cursors, unknown number of iterations | ★★★☆☆ |
| Do Until … Loop | Do Until rs.EOF … rs.MoveNext … Loop | Same as above (personal preference) | ★★★☆☆ |
6. Teacher Summary – VBScript Looping in Classic ASP
VBScript Looping (inside Classic ASP) means:
- For Each … Next → cleanest & safest — use whenever possible
- For i = … To … Next → when you need position/index/count
- Do While Not rs.EOF … rs.MoveNext … Loop → classic ADO database pattern
- Always check for empty collections (If Not rs.EOF Then or If UBound(arr) >= 0 Then)
- Use Server.HTMLEncode on dynamic content inside loops
- No modern foreach with index — you had to use For i or calculate manually
This is how millions of dynamic tables, menus, product lists, and search results were built 1998–2010 — and many legacy Indian business/internal systems still run exactly this looping style in 2026.
Next class?
- Want a full shopping cart example with loops + totals?
- Or pagination with For … Next (classic style)?
- Or compare VBScript loops vs Razor @for / @foreach side-by-side?
- Or move to the next W3Schools topic (ASP Redim or ASP Procedures)?
Just tell me — I’m here! 🚀🇮🇳 Keep going strong, Webliance! 😊
