Keyword For - leonard-thieu/monkey GitHub Wiki
Declares the start of a For/Next loop.
Syntax
For Local IndexVariable := FirstValue To LastValue [ Step StepValue ] Statements... Next
For Local IndexVariable := FirstValue Until LastValue [ Step StepValue ] Statements... Next
For Local IndexVariable := EachIn Collection 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
Examples
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