Bounded Loops

Describes range and key iteration bounded loops

Bounded loops allow you to execute a set of statements a number of times which is known before entering the loop. The number of times might be determined by a range or a collection.

Range iteration (for in)

This loop iterates over an integer range. That is, on each iteration, an integer variable increments by one:
for <id> in <rangeStart>..<rangeEnd> do
    <statements>
end
Note: rangeStart and rangeEnd can be expressions. The range limits are inclusive. Modifying the variable inside the loop does not have any effect on the loop's execution.
For example, this loop displays the numbers from 1 to 3, inclusive:
for i in 1..3 do
    display i
end

Key iteration (for in)

The for in loop can also iterate over the keys or indexes of an array:
for <id> in <expression> do
    <statements>
end
Note: expression must yield an array.
This example displays all the keys in the ages array:
ages as Int[String]
ages = ["John" : 23, "Peter" : 42, "Mary" : 29]

for name in ages do
    display name
end

Element iteration (for each in)

The for each in loop iterates over the values of an array. Some of them may be excluded by adding a "where" clause:
for each <id> in <expression> [where <condition>] do
    <statements>
end
Note: expression must yield an array.
This example displays all the values in the ages array:
ages as Int[String]
ages = ["John" : 23, "Peter" : 42, "Mary" : 29]

for each age in ages do
    display age
end
The following example shows only the ages above 25:
ages as Int[String]
ages = ["John" : 23, "Peter" : 42, "Mary" : 29]
	
for each age in ages
    where age > 25
do
    display age
end

Stopping a Loop

Loop execution can be stopped by using an Exit Statement. Execution will continue from the first statement following the end of the loop.