<!--
  - Authors:
  -   Denys Duchier     <duchier@ps.uni-sb.de>
  -
  - Copyright:
  -   Denys Duchier, 1999
  -
  - Last change:
  -   $Date: 2004-05-18 16:14:00 +0200 (Tue, 18 May 2004) $ by $Author: duchier $
  -   $Revision: 15954 $
  -
  - This file is part of Mozart, an implementation
  - of Oz 3
  -    http://www.mozart-oz.org
  -
  - See the file "LICENSE" or
  -    http://www.mozart-oz.org/LICENSE.html
  - for information on usage and redistribution
  - of this file, and for a DISCLAIMER OF ALL
  - WARRANTIES.
  -
  -->
<chapter id=chapter.official>
  <title>Official Loop Notation</title>
  <p>In order to provide convenient syntax for loops, two new keywords
have been introduced in Mozart&nbsp;1.1.0: <code/for/ and <code/do/.
Thus, a new statement is introduced in the Oz language and its syntax is:
<code display/for ?{Declarations} do ... end/
where ?{Declarations} is a sequence of 0 or more iterator and feature
declarations.   An iterator has the form:
<code>?{Pat} in ?{Generator}</code> 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.
</p>
  <section><title/Iterators/
    <p>These are the iterators officially supported starting in Mozart&nbsp;1.1.1.
For simplicity below, we write <code/X/ rather than <code/Pat/.
<list>
      <entry><code/X in ?{L}/
      <item>iterates over the elements of list ?{L}.  At each
iteration, <code/X/ is bound to the next element in ?{L}.  The
generator runs out when all elements in ?{L} have been consumed.
      <entry/<code/X in  ?{E1};?{E2};?{E3}//
      <entry/<code/X in (?{E1};?{E2};?{E3})//
      <item/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 <code/false/./
      <entry/<code/X in ?{E1}..?{E2};?{E3}//
      <item/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}./
      <entry/<code/X in ?{E1}..?{E2}//
      <item/same as above, but with increment of 1./
      <entry/<code/X in ?{E1};?{E2}//
      <item/shorthand for <code/X in ?{E1};true;?{E2}//
    </list></p>
  </section>
  <section><title/Features/
    <p>Additional loop functionality is made available through the new
notion of loop <q/features/.  For example, in C, it is possible to <code
proglang=cc/break/ or <code proglang=cc/continue/ the current loop.  We
generalize this idea by making the <q/break/ and <q/continue/ capabilities
first-class using nullary procedures: thus you can break or continue any loop
from any arbitrary level of dynamic nesting.
<list>
      <entry/<code/break:B//
      <item/binds <code/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 <q/break/ exceptions,
it may not work in a future release./
      <entry/<code/continue:C//
      <item/binds <code/C/ to a nullary procedure which, when invoked
immediately goes on to the next iteration of the loop.  The same warning
applies as above./
    </list></p>
  </section>
</chapter>
<!--
Local Variables:
mode: sgml
mode: auto-fill
sgml-default-dtd-file: "chapter.ced"
sgml-auto-activate-dtd: t
adaptive-fill-mode: nil
End:
-->
