Name
For . . . Next Statement
Syntax
Forcounter
=initial_value
Tomaximum_value
Stepstepcounter]
code to execute on each iteration
[Exit For] Next
-
counter
Use: Required (optional with
Next
statement)Data Subtype: Numeric
Any valid numeric variable to be used as the loop counter.
-
initial_value
Use: Required
Data Subtype: Numeric
Any valid numeric expression that specifies the loop counter’s initial value.
-
maximum_value
Use: Required
Data Subtype: Numeric
Any valid numeric expression that specifies the loop counter’s maximum value.
-
stepcounter
Use: Optional (required if
Step
used)Data Subtype: Numeric
Any valid numeric expression that indicates how much the loop counter should be incremented with each new iteration of the loop.
Description
Defines a loop that executes a given number of times, as determined
by a loop counter. To use the For...Next
loop, you
must assign a numeric value to a counter variable. This counter is
either incremented or decremented automatically with each iteration
of the loop. In the For
statement, you specify the
value that is to be assigned to the counter initially and the maximum
value the counter will reach for the block of code to be executed.
The Next
statement marks the end of the block of
code that is to execute repeatedly, and also serves as a kind of flag
that indicates the counter variable is to be modified.
Rules at a Glance
If
maximum_value
is greater thaninitial_value
, and noStep
keyword is used or the step counter is positive, theFor...Next
loop ...
Get VBScript in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.