next up previous contents
Next: The if Statement Up: Statements Previous: The fail Statement   Contents

The foreach Statement

$$ Foreach-Statement ::= "foreach" Variable "in" Expression ":" {Statement}
$$                       "end" ["foreach"] ";" .

You may wish to manipulate all elements of a list or a record sequentially in one rule path. For this purpose, the foreach statement was introduced. It has the following format:

foreach $var in expr: statements end foreach;

Sequentially the first, second, third, $...$ element of the list expr are assigned to $var and the statement sequence statements is executed for each of those assignments.

Every time the statements are being walked through, the variable $var is defined again. Its scope is the block statements.

The foreach statement also works for records. In that case, the variable $var is assigned the first, second, $...$ attribute name of the record expr.

The foreach statement also works for numbers:


next up previous contents
Next: The if Statement Up: Statements Previous: The fail Statement   Contents
Bjoern Beutel