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.

asp

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)

asp

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):

asp

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.

asp

Do Until version (also very common):

asp

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! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *