ASP For and Do Loops

Learn ASP's two core loop types — the counted For...Next loop and the condition-based Do...Loop — and when each one is the right tool.

The For...Next Loop

A For...Next loop is best when you know exactly how many times you want to repeat something. You give it a starting value, an ending value, and it runs the body once for every step in between, automatically increasing (or decreasing) the loop variable for you.

Example: For...Next with Step

<%
Dim i

' Counting up one at a time
For i = 1 To 5
    Response.Write("Up: " & i & "<br>")
Next

' Counting down by 2
For i = 10 To 0 Step -2
    Response.Write("Down: " & i & "<br>")
Next
%>
  • The loop variable (i above) is created and updated automatically — you never write i = i + 1 yourself.
  • If you leave out Step, ASP assumes Step 1.
  • Step can be negative to count downward, as in the second loop above.
  • Exit For immediately stops the loop and jumps to the line after Next, useful once you have found what you were looking for.

The Do While and Do Until Loops

Sometimes you do not know in advance how many times a loop needs to run — you just know the condition that should keep it going, or the condition that should stop it. That is what Do While and Do Until are for. Both check their condition before each pass through the loop body, so if the condition is never true (for Do While) or already true (for Do Until), the body might not run at all.

Example: Do While with Exit Do

<%
Dim counter
counter = 1

Do While counter <= 100
    Response.Write("Counter is " & counter & "<br>")
    If counter = 5 Then
        Exit Do
    End If
    counter = counter + 1
Loop
%>

Example: Do Until

<%
Dim total, nextNumber
total = 0
nextNumber = 1

Do Until total >= 100
    total = total + nextNumber
    nextNumber = nextNumber + 1
Loop

Response.Write("Reached " & total & " after adding " & (nextNumber - 1) & " numbers.")
%>
Note: A Do While or Do Until loop only stops when its condition changes, so you must update the variable it depends on somewhere inside the loop body. Forgetting that step creates an infinite loop, which will hang the request until the server times it out.
Loop TypeRuns WhenTypical Use
For...NextYou know the exact number of repetitionsPrinting a numbered list, looping a fixed number of times
Do While...LoopYou want to keep going while a condition stays trueReading records until you run out of data
Do Until...LoopYou want to keep going until a condition becomes trueAccumulating a total until it passes a target

Do...Loop also supports a post-condition form, where the check happens at the bottom instead of the top: `Do ... Loop While condition` or `Do ... Loop Until condition`. Because the condition is checked after the body runs, this version always executes the loop body at least once, even if the condition was already false (or true) to begin with.

Note: Both loop families support Exit For and Exit Do to break out early. Later on you will also meet For Each...Next, a variant built for stepping through every item in a collection, such as every key in Request.QueryString, without tracking an index yourself.

Exercise: ASP Control Flow

Which statement closes a multi-line If block in VBScript?