Declares the start of a For/Next loop.
For Local IndexVar := start_value To end_value [ Step constant_value ]
Statements...
Next
For Local IndexVar := start_value Until end_value [ Step constant_value ]
Statements...
Next
For Local IndexVar := Eachin value_set
Statements...
Next
For declares the start of a For/Next loop, allowing for iteration through a set of values or objects.
There are three variants of the For/Next loop, modifying the values of the index variable via the range keywords below:
To
: Iterates through the given start and end values;Until
: This variant of For/Next stops iterating before the last value in the loop, useful for iterating through zero-based arrays. (See Example 3.)Eachin
: Iterates through a set of objects or values, such as a list or array;
Alternatives to the closing Next keyword are End For, or simply End.
For
k
=
1
To
1000
Print
k
Next
' With optional Step
For
k
=
1
To
1000
Step
100
Print
k
Next
' Alternative syntax examples
For
k
=
1
To
1000
Print
k
End
For
k
=
1
To
1000
Print
k
End
For
' Assume m:MyObject and mylist:List
For
m
=
Eachin
mylist
Print
m.myField
Next
' Assume i:Int and myarray:Int[]
For
i
=
Eachin
myarray
Print
myarray
[
i
]
Next
NUM
=
100
' Assume myarray[NUM] has been declared
' Prints 0 to 99 without requiring NUM-1
For
k
=
0
Until
NUM
Print
myarray
[
k
]
Next