For...Next


Statement For...Next



Syntax 1 For counter = start expression To stop expression (Step step expression)

[statements]

Next


Remarks

FOR is a looping construct that requires you to explicitly set a number of iterations at the top of the loop. The start, stop and step must consist of numeric expressions, and may include variables.

The loop begins with FOR and ends at the paired NEXT.


The loop will execute when the stop condition is met, usually when the counter variable and the stop expression are equal.


It is possible to make the counter count down, by making step a negative value.


It is also possible to write endless loops with FOR-NEXT, especially when using variables for the start, stop and step expressions.


Everything that appears on the line after the NEXT is considered a comment.


See Also:

While


Example Script


' An example of using expressions for start, stop and next expressions

' It would be easy to inadvertently make an endless loop script in this way.

FOR i = a TO b STEP c

.....

NEXT


' An example setting up 10 iterations through a loop

FOR i = 0 to 9

......

NEXT i ' Note that the variable name is optional.


' Another example of setting up 10 iterations through a loop

FOR i = 9 to 0 STEP -1

.....

NEXT