Subsections


Iteration Statements

The iteration statements are based on the following constructs:

while statement

Syntax: while ( cond_expr ) statement
where cond_expr is a boolean expression, and statement any statement: an expression statement, an iteration statement, a compound statement, a selection statement, a function definition statement, a jump statement or an empty statement.

Semantics: The statement is executed while the boolean expression cond_expr is evaluated to true. Note that a while statement does not return any atom

The following table presents several examples of while statements:
while Statement Examples
while (true) a++;
a is increment definitively!
while (n-) a++;
an error is raised: boolean expected, got integer
while (n- > 0) a++;
this is better
while (n++ <= 100 || stop) {if (!perform(a++)) break; check(a);}
note the usage of a compound statement and of the break
while (name != "john") {l := (select Person.name = name); name := get_name();}
no comments

do/while statement

Syntax: do statement while ( cond_expr )
where cond_expr is a boolean expression, and statement any statement: an expression statement, an iteration statement, a compound statement, a selection statement, a function definition statement, a jump statement or an empty statement.

Semantics: The statement is executed at least once. Then while the boolean expression cond_expr is evaluated to true, the statement is executed. Note that a do/while statement does not return any atom

The following table presents several examples of do/while statements:
do/while Statement Examples
do a++; while (true);
a is increment definitively!
do a=+; while (n-);
an error is raised: boolean expected, got integer
do a++; while (n- > 0);
this is better
do {if (!perform(a++)) break; check(a);} while (n++ <= 100 || stop);
note the usage of a compound statement and of the break
do {l := (select Person.name = name); name := get_name();} while (name != "john");
no comments

C-for statement

Syntax: for ( [expr1] ; [cond_expr] ; [expr2] ) statement
where cond_expr is a boolean expression, expr1 and expr2 are any expressionss and statement any statement: an expression statement, an iteration statement, a compound statement, a selection statement, a function definition statement, a jump statement or an empty statement.

Semantics: The expression expr1 is evaluated. While the boolean expression cond_expr is evaluated to true, the statement is executed and the expression expr2 is evaluated.
Note that a C-for statement does not return any atom

The following table presents several examples of C-for statements:
C-for Statement Examples
for (x := 0; x < 100; x++) a++;
increment a an hundred times
for (x := 0, y := 1; x < 100 && check(y); x++) {y := get(y); if (y == 9999) break;}
a more complex example
for (x := 100; x; x-) perform(x);
raises an error: boolean expected, got integer
for (x := 0;;) doit();
note that there is neither a conditionnal expression, nor a second expression
for (;;) doit();
same as while(true)

collection-for statement

Syntax: for ( var in expr ) statement
where cond_expr is a boolean expression, var denotes the name of a variable, expr is an expression of type collection and statement any statement: an expression statement, an iteration statement, a compound statement, a selection statement, a function definition statement, a jump statement or an empty statement.

Semantics: For each element in the collection denoted by expr, the variable var is assigned to this element and the statement is executed. Note that a collection-for statement does not return any atom

The following table presents several examples of collection-for statements:
collection-for Statement Examples
for (x in list(1, 2, 3)) a += x;
increments a with 1, 2 and 3
for (x in (select Person)) names += x.name;
concatenates all the person names
for (x in (select Person.name = "john")) if (x.age < 10 || x.spouse.age < 10) throw "cannot mary children!!";
a moralistic example
for (x in 1) doit();
raises an error: boolean expected, got integer

EyeDB manual