ASP/VBScript For … Next Repeats a group of statements a specified number of times.
ShotDev Focus:
- Using Asp and For…Next
Syntax
For counter = start To end [Step step] [statements] [Exit For] [statements] Next
The For…Next statement syntax has these parts:
Part | Description |
counter | Numeric variable used as a loop counter. The variable can’t be an array element or an element of a user-defined type. |
start | Initial value of counter. |
end | Final value of counter. |
step | Amount counter is changed each time through the loop. If not specified, step defaults to one. |
statements | One or more statements between For and Next that are executed the specified number of times. |
Remarks
The step argument can be either positive or negative. The value of the step argument determines loop processing as follows:
Value | Loop executes if |
Positive or 0 | counter <= end |
Negative | counter >= end |
Once the loop starts and all statements in the loop have executed, step is added to counter. At this point, either the statements in the loop execute again (based on the same test that caused the loop to execute initially), or the loop is exited and execution continues with the statement following the Next statement.
|
|
Tip Changing the value of counter while inside a loop can make it more difficult to read and debug your code. | |
|
Exit For can only be used within a For Each…Next or For…Next control structure to provide an alternate way to exit. Any number of Exit For statements may be placed anywhere in the loop. Exit For is often used with the evaluation of some condition (for example, If…Then), and transfers control to the statement immediately following Next.
Example
asp_for_next.asp
<% Option Explicit %> <html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <% Dim i For i = 0 To 5 Response.write(i &" ShotDev.Com <br>") Next Response.write("<hr>") For i = 0 To 10 Step 3 Response.write(i &" ShotDev.Com <br>") Next Response.write("<hr>") For i = 10 To 0 Step -2 Response.write(i &" ShotDev.Com <br>") Next Response.write("<hr>") %> </body> </html>
Create a asp file and save to path root-path/myasp/
Run
http://localhost/myasp/asp_for_next.asp
Screenshot