Keyword For

Declares the start of a For/Next loop.

Syntax

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

Description

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;
In the To and Until variants, the optional Step keyword, followed by a constant value, allows control over the size of the iteration step.

Alternatives to the closing Next keyword are End For, or simply End.

See also

Next | To | Eachin | Until

Example

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 

Example 2

' 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 

Example 3

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