Use For...Next in Classic ASP (VBScript) to repeat a block of statements while a numeric counter moves from a starting value toward an ending value. The Step value controls how much the counter changes each time.
Dim i
For i = 1 To 5
Response.Write i & " "
Next
Output:
1 2 3 4 5
The loop starts with i = 1, displays each value, then increases i by the default step of 1. The ending value is included if the counter reaches it. See Do...Loop if the number of repetitions is not known in advance.
For counter = startValue To endValue [Step stepValue]
' statements to repeat
Next
The square brackets in the syntax mean that Step stepValue is optional; do not type the brackets into executable VBScript. The counter can be declared with Dim and is numeric.
| Part | Meaning |
|---|---|
counter | Numeric variable that changes after each pass through the loop. |
startValue | The first value assigned to the counter. |
endValue | The limit checked before each pass. The loop includes this value only if it is reached. |
Step stepValue | Optional increment or decrement; defaults to 1. |
Next | Ends the loop body, updates the counter, and checks whether another pass is needed. |
This preserves the original example from the tutorial. Each number appears on a separate line because Response.Write outputs the HTML <br> tag.
Dim i
For i = 1 To 10
Response.Write i & "<br>"
Next
Output:
1
2
3
4
5
6
7
8
9
10
The loop stops after displaying 10. In a full .asp file, put the VBScript statements between <% and %> as shown in the complete examples below.
The original code uses Step 2 (not 3), producing the sequence 1, 3, 5, 7, 9. The prose has been corrected to match the code.
Dim i
For i = 1 To 10 Step 2
Response.Write i & "<br>"
Next
Output:
1
3
5
7
9
Dim i
For i = 1 To 10 Step 3
Response.Write i & " "
Next
Output:
1 4 7 10
The step does not have to divide the range evenly. For example, For i = 1 To 9 Step 3 displays 1, 4 and 7; it stops before 10 because 10 is beyond the ending value.
Dim i
For i = 2 To 10 Step 2
Response.Write i & " "
Next
Output:
2 4 6 8 10
When counting downward, use a negative step and a starting value greater than or equal to the ending value.
Dim i
For i = 10 To 1 Step -1
Response.Write i & " "
Next
Output:
10 9 8 7 6 5 4 3 2 1
| Loop | Result | Reason |
|---|---|---|
For i = 1 To 3 | 1, 2, 3 | Default positive step. |
For i = 3 To 1 Step -1 | 3, 2, 1 | Negative step counts down. |
For i = 5 To 1 | No output | Positive step cannot reach a lower end value. |
For i = 1 To 5 Step -1 | No output | Negative step moves away from a higher end value. |
Use Exit For after a condition becomes true. It stops the current loop immediately and continues at the statement following Next.
Dim i
For i = 1 To 10
If i = 6 Then
Exit For
End If
Response.Write i & " "
Next
Response.Write "<br>Loop finished"
Output:
1 2 3 4 5
Loop finished
Classic VBScript does not support the newer Visual Basic Continue For statement. To avoid processing an item without ending the loop, put the work inside an If...Then condition.
Dim i
For i = 1 To 5
If i <> 3 Then
Response.Write i & " "
End If
Next
Output:
1 2 4 5
A loop can contain another loop. Use a different counter variable for each level. In this example, the outer loop controls rows and the inner loop controls columns.
Dim rowNum, colNum
For rowNum = 1 To 3
For colNum = 1 To 3
Response.Write (rowNum * colNum) & " "
Next
Response.Write "<br>"
Next
Output:
1 2 3
2 4 6
3 6 9
The inner loop completes three passes for every pass of the outer loop, so the body runs 3 × 3 = 9 times. Use small, deliberate ranges on public-facing pages to avoid excessive work.
A VBScript array has bounds. The LBound() and UBound() functions make a numeric loop work without hard-coding the last index.
Dim countries, i
countries = Array("UK", "USA", "Canada", "UAE")
For i = LBound(countries) To UBound(countries)
Response.Write Server.HTMLEncode(countries(i)) & "<br>"
Next
Output:
UK
USA
Canada
UAE
When you only need to read every item and not its index, For Each is often simpler:
Dim countries, country
countries = Array("UK", "USA", "Canada", "UAE")
For Each country In countries
Response.Write Server.HTMLEncode(country) & "<br>"
Next
See Filter() for selecting matching values from an array, and Join() for displaying values as a single string.
Dim i, total
total = 0
For i = 1 To 10
total = total + i
Next
Response.Write "Sum = " & total
Output:
Sum = 55
Classic ASP can generate repeated HTML rows. Keep the table structure outside the loop and write one table row for each iteration.
<table class="table table-bordered">
<thead><tr><th>Number</th><th>Square</th></tr></thead>
<tbody>
<%
Dim i
For i = 1 To 5
Response.Write "<tr><td>" & i & "</td><td>" & (i * i) & "</td></tr>"
Next
%>
</tbody>
</table>
| Number | Square |
|---|---|
| 1 | 1 |
| 2 | 4 |
| 3 | 9 |
| 4 | 16 |
| 5 | 25 |
Copy the following into a test file named for-example.asp on an IIS server with Classic ASP enabled. It shows the full page structure with <% and %> delimiters.
<%@ Language="VBScript" %>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>For Next example</title></head>
<body>
<h1>Counting with For Next</h1>
<%
Dim i
For i = 1 To 5
Response.Write "<p>Number " & i & "</p>"
Next
%>
</body>
</html>
This sample creates five paragraphs, from Number 1 through Number 5. Unlike the copyable small snippets above, it includes the whole file structure needed for standalone execution.
| Issue | How to resolve it |
|---|---|
| Nothing appears | Check the direction of the loop: counting down requires Step -1. |
| Too many iterations | Check the start, end, and step values. Avoid Step 0, which can keep the counter unchanged and repeat indefinitely. |
| Unexpected output | Check whether Response.Write includes a separator such as " " or "<br>". |
| Loop stops earlier than expected | Check for Exit For and remember that the end value is not printed if the step jumps past it. |
| Invalid syntax | Use For ... To ... [Step ...] with a matching Next; VBScript does not use PHP-style braces for this loop. |
No. Without an explicit Step, the counter increases by 1 after each iteration.
Yes, if the counter actually reaches it. For example, 1 through 10 with Step 2 ends at 9.
For...Next uses a numeric counter and is helpful when you need an index or a fixed range. For Each visits each element of an array or collection without needing to manage its index.
Exit For exits only the innermost active For loop. Any outer loop continues unless you explicitly handle its exit.
Use Do...Loop when repetitions depend on a condition rather than a planned numerical range. For conditional branching within loops, see If...Then...Else.
For the complete statement syntax, see the Microsoft For...Next reference.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| robotran | 31-07-2014 |
| very helpful | |
28-08-2020 | |
| <% fromDate="1-Aug-2020" toDate="28-Aug-2020" nood=DateDiff("y",fromDate,toDate) for i = 0 to nood ddt =(DateAdd("d",i,fromdate)) response.write day(ddt) &"-" & mid(monthname(month(ddt)),1,3)&"-"&Year(ddt) & "<bR>" next %> | |