REDUCE

5.6 REPEAT … UNTIL

repeatuntil is very similar in purpose to whiledo. Its syntax is:

repeat \(\langle \)statement\(\rangle \) until \(\langle \)boolean expression\(\rangle \)

(PASCAL users note: Only a single statement – usually a group statement – is allowed between the repeat and the until.)

There are two essential differences:

1.
The test is performed after the controlled statement (or group of statements) is executed, so the controlled statement is always executed at least once.
2.
The test is a test for when to stop rather than when to continue, so its “polarity” is the opposite of that in whiledo.

As an example, we rewrite the example from the while …do section:

    ex:=0; term:=1;
    repeat <<ex := ex+term; term := (term + term^2)/3>>
       until num(term - 1/1000) < 0;
    ex;

In this case, the answer will be the same as before, because in neither case is a term added to ex which is less than 1/1000.


Hosted by Download REDUCE Powered by MathJax