For...Next Statement
Language Items List
Definition:
Repeats the execution of a statement block a specified number of times.
Syntax:
For counter = start To end [Step increment]
[statementblock]
[Exit For]
[statementblock]
Next [counter [,counter] [,...]]
Syntax Description
For Marks the beginning of a For...Next loop control structure. Must always be the
very first part of the structure.
counter A numeric variable used to count the repetitions of the loop.
start Specifies the initial value assigned to counter.
To Separates the values of start and end.
end Specifies the final value assigned to counter.
Step Controls loop execution and indicates that increment is specified explicitly.
increment Amount counter is changed each time the loop is executed. The increment defaults to 1 if
you do not specify Step explicitly.
statementblock One or more program lines placed between For and Next. These lines are
executed the specified number of times.
Exit For Provides an alternative way to exit the For...Next control structure. It can
be used only within a For...Next control structure. You can place any number of
Exit For statements in the For...Next loop. Exit For immediately transfers
control to the statement following the Next keyword, thus is often used with the
evaluation of some condition such as If...Then.
Next Marks bottom of statement block for a For...Next loop and causes increment to be added to counter.
To begin a For...Next loop counter is initialized to start. If increment is positive or 0, the statement block between For and Next will execute if counter is less than or equal to end. When increment is negative, the statement block between For and Next will execute if counter is greater than or equal to end. The statement block is not executed and the loop is exited if increment is positive and counter is greater than end, or when increment is negative and counter is less than end.
The Step increment is added to counter only when the Next keyword is reached. At this point, either the loop is
repeated or exited based on the conditions listed in the previous paragraph If the
loop is exited, execution continues with the statement immediately following
the Next statement.
Changing the value of counter is allowed inside the loop, however, this makes the execution of the loop
difficult to predict, making the program more difficult to read and debug.
For...Next loops can be nested by placing one For...Next loop within another.
This is useful when using arrays. Be sure to give a unique variable name to
each loop. This variable name serves as the loop counter. Then close each loop in reverse order as shown in the following example:
The following construction is correct to initialize a 2x10 array:
Dim array(2,10) As Integer
For I = 1 to 2
For J = 1 To 10
array(I,J) = I*10 + J
Next J
Next I
The assignment statement will execute 2*10 = 20 times.
Note: If you leave out the variable in a Next statement, the Step increment value
is added to the variable that is associated with the most recent For statement.
If a Next statement is encountered before its corresponding For statement, a
"Next without For exception" is thrown.
See Also:
Do...Loop
Exit
While...Wend