For...Next loops in VBScript

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.

Quick example: print numbers 1 to 5

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...Next loop syntax

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.

PartMeaning
counterNumeric variable that changes after each pass through the loop.
startValueThe first value assigned to the counter.
endValueThe limit checked before each pass. The loop includes this value only if it is reached.
Step stepValueOptional increment or decrement; defaults to 1.
NextEnds the loop body, updates the counter, and checks whether another pass is needed.

Count from 1 to 10

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.

Change the increment using Step

Step 2: print odd numbers

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

Step 3: increase by three

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.

Step 2: print even numbers

Dim i
For i = 2 To 10 Step 2
    Response.Write i & " "
Next

Output:

2 4 6 8 10

Count backward using a negative Step

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
LoopResultReason
For i = 1 To 31, 2, 3Default positive step.
For i = 3 To 1 Step -13, 2, 1Negative step counts down.
For i = 5 To 1No outputPositive step cannot reach a lower end value.
For i = 1 To 5 Step -1No outputNegative step moves away from a higher end value.

Stop the loop early with Exit For

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

Skip one value with If...Then

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

Nested For...Next loops

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.

Use For...Next with an array

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.

Practical uses of For...Next

Find the sum of numbers from 1 to 10

Dim i, total
total = 0
For i = 1 To 10
    total = total + i
Next
Response.Write "Sum = " & total

Output:

Sum = 55

Build a simple HTML table

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>
NumberSquare
11
24
39
416
525

Complete Classic ASP page example

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.

Troubleshooting and frequently asked questions

IssueHow to resolve it
Nothing appearsCheck the direction of the loop: counting down requires Step -1.
Too many iterationsCheck the start, end, and step values. Avoid Step 0, which can keep the counter unchanged and repeat indefinitely.
Unexpected outputCheck whether Response.Write includes a separator such as " " or "<br>".
Loop stops earlier than expectedCheck for Exit For and remember that the end value is not printed if the step jumps past it.
Invalid syntaxUse For ... To ... [Step ...] with a matching Next; VBScript does not use PHP-style braces for this loop.

Is Step mandatory?

No. Without an explicit Step, the counter increases by 1 after each iteration.

Does For...Next include the ending value?

Yes, if the counter actually reaches it. For example, 1 through 10 with Step 2 ends at 9.

What is the difference between For...Next and For Each?

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.

Can Exit For stop nested loops?

Exit For exits only the innermost active For loop. Any outer loop continues unless you explicitly handle its exit.

When should I use Do...Loop instead?

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.

Back to top ↑


ASP Home



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
%>



✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer