While
rascal-0.40.17
Synopsis
While loop.
Syntax
while (Exp)
Statement
while (Exp) {
Statements
}
Description
The Boolean expression Exp is evaluated repeatedly and Statement is executed when the test is true.
Execution ends the first time that the test yields false.
The test Exp is executed from scratch in each repetition and only the first true
value (if any) is used.
This is relevant when Exp contains a Match or NoMatch operator.
By default, the value of a while statement is the empty list. In general, the value of a while statement consists of all values contributed by Append statements that are executed during the repeated execution of its body Statement.
Examples
rascal>import IO;
ok
rascal>int n = 3;
int: 3
rascal>while( n > 0 ) {
>>>>>>> println("n = <n>"); n -= 1;
>>>>>>>}
n = 3
n = 2
n = 1
list[void]: []
Now build a list result using the append
statement:
rascal>n = 3;
int: 3
rascal>while (n > 0) {
>>>>>>> append n * n; n -= 1;
>>>>>>>}
list[int]: [9,4,1]
Just to be sure, a Comprehension is the superior way to write this:
rascal>[n * n | n <- [3 .. 0]];
list[int]: [9,4,1]