HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Classic ASPVariablesStringsConditions (If / Select Case)Loops (For / Do While)Form and Request
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
✍️ Text & Number
🔁 Condition & Loop
🧩 Functions & Forms
Classic ASP›Loops (For / Do While)

Loops (For / Do While)

Counting repetition with For-Next, conditional repetition with Do While.

Simple enough for a 5-year-old to understand

For ... Next

Example code
1
Dim i
2
For i = 1 To 5
From 1 to 5, including 5.
LoopFrom 1 to 5, including 5.
3
Response.Write i
Prints the number in each iteration.
Output / InputPrints the number in each iteration.
4
Next
End of the loop.
LoopEnd of the loop.
LoopOutput / Input
💡 Hint: Step minus one is used to count backwards.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Which keyword is used to close a For loop?

About this topic

There are four loop structures in VBScript: the counter-based loop, the collection-traversing loop, and loops with condition checks at the beginning or end. Each has its own closing keyword.

The boundaries of the counter-based loop are inclusive, and a step value can be specified. Modifying the counter inside the loop body is not forbidden by the language, but it makes the loop's behavior untrackable and should be avoided.

In ASP, loops are mostly used to traverse database records. Forgetting to advance the recordset in this pattern leads to an infinite loop and causes the server to keep spinning until a timeout occurs.

What you will learn

  • The counter-based loop
  • The collection loop
  • Conditional loops
  • Early exit
  • The recordset traversal pattern

Who is this for?

  • ASP developers listing databases
  • Those dealing with infinite loops
  • Those getting page timeouts

When to use?

When traversing recordsets, the loop that tests the condition at the beginning, along with the end-of-file indicator, is the standard pattern.

If the number of repetitions is known, a counter-based loop is used; since the language handles the progression, there is no risk of an infinite loop.

Common mistakes

Wrong

<%
Option Explicit
Dim kayitlar
Set kayitlar = baglanti.Execute("SELECT ad FROM uyeler")
Do While Not kayitlar.EOF
  Response.Write kayitlar("ad") & "<br>"
Loop
%>

Since the recordset is not advanced, the end-of-file indicator never becomes true; the loop writes the same record forever and the page times out. You need to move to the next record on each iteration. This is the most common infinite loop in ASP.

Correct

<%
Option Explicit
Dim kayitlar
Set kayitlar = baglanti.Execute("SELECT ad FROM uyeler")
Do While Not kayitlar.EOF
  Response.Write kayitlar("ad") & "<br>"
  kayitlar.MoveNext
Loop
%>

Wrong

<%
Option Explicit
Dim i
For i = 1 To 5
  Response.Write i
Loop
%>

The closing keyword for the counter-based loop is different; a syntax error occurs because the conditional loop's closing is written instead. Each of the four loop types has its own ending, and mixing them up immediately generates an error.

Correct

<%
Option Explicit
Dim i
For i = 1 To 5
  Response.Write i
Next
%>

Wrong

<%
Option Explicit
Dim i
For i = 1 To 10
  If i = 5 Then
    i = 8
  End If
  Response.Write i & " "
Next
%>

Since the counter is modified inside the body, the number of iterations the loop will take becomes unclear to the reader, and the output is unexpected. VBScript allows this, but it leads to bugs that are very hard to track down in maintained code; if skipping is necessary, a separate variable should be used.

Correct

<%
Option Explicit
Dim i, deger
For i = 1 To 10
  deger = i
  If i = 5 Then
    deger = 8
  End If
  Response.Write deger & " "
Next
%>

Frequently asked questions

How do I exit a loop early?

Each loop type has its own exit statement that terminates the loop immediately. There is no corresponding statement to skip a single iteration; instead, the body must be wrapped with a condition.

How do I count backwards?

In a counter-based loop, the step value is given as negative and the start is greater than the end. If no step is specified, it increments by one; loops that decrease can be written with a negative step.

Where is the collection loop used?

In arrays, form fields, and object collections. Since there is no need to calculate indices, it eliminates the possibility of out-of-bounds errors and is the cleanest way to traverse form fields.

Summary

The four loop types have distinct endings.

Boundaries are inclusive in the counter-based loop.

It is mandatory to advance the recordset.

Modifying the counter in the body should be avoided.

Each loop has its own exit statement.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code