Repetition

Repetition can be expressed in a number of ways. An important paradigm of repetition is the while-loop, e.g.


\begin{elan}
WHILE
the word does not occur on this page
REP
look at the following page
ENDREP
\end{elan}

The condition between WHILE and REP is a unit which, upon evaluation, yields a truth value (true or false).

The paragraph between the keywords REP and ENDREP is executed only if the condition yields true. After the execution of the paragraph (the ``loop-body'') the condition is evaluated again, and accordingly the loop-body may be executed repeatedly. The execution ends as soon as the condition upon evaluation yields false. For those who prefer more readable keywords, REP may be also written as REPEAT, and ENDREP as ENDREPEAT.


\begin{elan}
WHILE
the sun is shining
REP
have a drink ;
sing another song
ENDREP
\end{elan}

In this kind of repetition a test of applicability is performed before each cycle (``pre-checked-loop''). If in this example it is rainy, the loop-body is never executed, and you have to stay sober.

For cases where there is always something to be done first, and checked for repetition afterwards (``post-checked-loop'') Elan has the following complementary repetitive construct:


\begin{elan}
REP
drink a glass of tea with rum ;
sing a christmas carol
UNTIL
it stops snowing
ENDREP
\end{elan}

In this case the condition which stands between UNTIL and ENDREP is executed after each execution of the first paragraph, the repetition continues if the condition yields false and stops if the condition yields true.

For cases where the number of repetitions is known in advance, Elan offers a ``counting-loop'', also called for-loop.


\begin{elan}
FOR i FROM min UPTO max
REP
tally i th entry
ENDREP
\end{elan}

The units min and max yielding integers are evaluated once at the beginning of the for-loop. The controlled integer variable i gets the value of min. As long as i is less or equal to max, the loop-body between REP and ENDREP is executed, followed by an increment of the ``loop-variable'' i by one. Within the loop-body i may be used, but not assigned to. Its value is undefined after the execution of the for-loop.

In case you want to count not upwards, but downwards from a larger value to a smaller one, there is a variant of the counting-loop:


\begin{elan}
FOR nr FROM stock DOWNTO minimum
REP
sell one
ENDREP
\end{elan}

In this variant, nr is counted down from stock to minimum. If stock was already below minimum, then the loop-body is not executed.

In case the value of the from-part is one and the controlled variable is of no interest, the for- and from-parts may be left out, leading to the shorter form


\begin{elan}
UPTO 20
REP
hit him over the head ;
pick him up
ENDREP
\end{elan}

A later section will demonstrate the use of the counting-loop in processing rows of data elements.