Skip to main content

For

rascal-0.34.0

Synopsis

For loop.

Syntax

for (Exp₁ , Exp₂, ..., Expₙ) 
Statement

for (Exp₁ , Exp₂, ..., Expₙ) {
Statements
}

Label: for (Exp₁ , Exp₂, ..., Expₙ)
Statement

Label: for (Exp₁ , Exp₂, ..., Expₙ) {
Statements
}

Description

The for-statement executes Statement for all possible combinations of values generated, and filtered, by the expressions Expᵢ.

Some of the expressions can generate bindings (Enumerator, <<Values,Boolean,Match>>), and some can filter them (Values). The for loop will iterate over the cartesian product of all the generating expressions, and filter the combinations which fail the conditional expressions.

By default, the value of a for statement is the empty list. In general, the value of a for statement consists of all values contributed by Append statements that are executed during the repeated execution of its body Statement.

For loops maybe be labeled for the benefit of labeled versions of Continue, Break and Fail.

Examples

rascal>import IO;
ok
rascal>for(int n <- [1 .. 5])
>>>>>>> println("n = <n>");
n = 1
n = 2
n = 3
n = 4
list[void]: []
rascal>for(int n <- [1 .. 5])
>>>>>>> append n * n;
list[int]: [1,4,9,16]