|- Up -||Next >>|
In order to provide convenient syntax for loops, two new keywords have been introduced in Mozart 1.1.0:
do. Thus, a new statement is introduced in the Oz language and its syntax is:
do ... end
where Declarations is a sequence of 0 or more iterator and feature declarations. An iterator has the form: Pat
in Generator where Generator describes how to generate the successive values for pattern Pat whose variables are local to the loop. The generators are stepped in parallel and the loop terminates as soon as one of the generators runs out of values.
These are the iterators officially supported starting in Mozart 1.1.1. For simplicity below, we write
X rather than
iterates over the elements of list L. At each iteration,
X is bound to the next element in L. The generator runs out when all elements in L have been consumed.
X in (E1
this iterator is intended to have a C-like flavor. E1 is the initial value, E2 is the boolean condition, and E3 is the next value. The iterator runs out when E2 evaluates to
iterate over the integers from E1 up to E2 inclusive, by increment of E3. E1, E2 and E3 are evaluated only once prior to starting the loop. Whether the loop is intended to count upward or downward is determined by the sign of E3.
same as above, but with increment of 1.
X in E1
Additional loop functionality is made available through the new notion of loop ``features''. For example, in C, it is possible to
continue the current loop. We generalize this idea by making the ``break'' and ``continue'' capabilities first-class using nullary procedures: thus you can break or continue any loop from any arbitrary level of dynamic nesting.
B to a nullary procedure which, when invoked immediately breaks out of the corresponding loop. This is currently implemented using an exception, but may change in the future, which means that (1) if you indiscriminately catch all exceptions you may break this functionality, (2) if your code relies on catching ``break'' exceptions, it may not work in a future release.
C to a nullary procedure which, when invoked immediately goes on to the next iteration of the loop. The same warning applies as above.
|- Up -||Next >>|