Repetition

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


#litout1684#

The <#1469#>condition<#1469#> between <#1470#>WHILE<#1470#> and <#1471#>REP<#1471#> is a unit which, upon evaluation, yields a truth value (true or false).

The paragraph between the keywords <#1472#>REP<#1472#> and <#1473#>ENDREP<#1473#> 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, <#1474#>REP<#1474#> may be also written as <#1475#>REPEAT<#1475#>, and <#1476#>ENDREP<#1476#> as <#1477#>ENDREPEAT<#1477#>.


#litout1686#

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:


#litout1688#

In this case the condition which stands between <#1482#>UNTIL<#1482#> and <#1483#>ENDREP<#1483#> 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.


#litout1690#

The units <#1486#>min<#1486#> and <#1487#>max<#1487#> yielding integers are evaluated once at the beginning of the for-loop. The controlled integer variable <#1488#>i<#1488#> gets the value of <#1489#>min<#1489#>. As long as <#1490#>i<#1490#> is less or equal to <#1491#>max<#1491#>, the loop-body between <#1492#>REP<#1492#> and <#1493#> ENDREP<#1493#> is executed, followed by an increment of the ``loop-variable'' <#1494#>i<#1494#> by one. Within the loop-body <#1495#>i<#1495#> 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:


#litout1692#

In this variant, <#1498#>nr<#1498#> is counted down from <#1499#>stock<#1499#> to <#1500#>minimum<#1500#>. If <#1501#>stock<#1501#> was already below <#1502#>minimum<#1502#>, 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


#litout1694#

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