FOR
The versatile FOR keyword can be used to iterate over a different collections like array, objects and set of HTML elements.
FOR-IN
A FOR-IN
statement iterates through all entries of an array, object, or values that implement Iterable
interface.
Each element returned by expression is visited exactly once. The current element is made available for further processing in the variable specified by variableName
.
This will iterate over all elements from the array and make the current array element available in variable i
. i
is not modified in this example but simply pushed into the result using the RETURN
keyword.
The variable introduced by FOR
is available until the scope the FOR
is placed in is closed.
Another example that uses a statically declared array of values to iterate over:
Nesting of multiple FOR
statements is allowed, too. When FOR
statements are nested, a cross product of the array elements returned by the individual FOR
statements will be created.
In this example, there are two array iterations: an outer iteration over the array of months plus an inner iteration over the array of years. The inner array is traversed as many times as there are elements in the outer array. For each iteration, the current values of months and years are made available for further processing in the variable m
and y
.
FOR-WHILE
FOR-WHILE
variant specifies the repeated iteration as long as a boolean condition evaluates to true. The condition is evaluated before each iteration. variableName
variable holds a number of each iteration.