Name
For . . . Next Statement
Syntax
Forcounter=initial_valueTomaximum_value[Stepstepcounter]code to execute on each iteration[Exit For] Next
counterUse: Required
Data Type: Numeric
A variable to be used as the loop counter.
initial_valueUse: Required
Data Type: Numeric
Any valid numeric expression that specifies the loop counter’s initial value.
maximum_valueUse: Required
Data Type: Numeric
Any valid numeric expression that specifies the loop counter’s maximum value.
stepcounterUse: Optional (required if
Stepused)Data Type: 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
initial_valueis greater thanmaximum_value, and noStepkeyword is used or the step counter is positive, theFor...Nextloop is ignored and execution commences with the first line of code immediately ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access