[Oberon] LOOP conversion

Michael Schierl schierlm at gmx.de
Mon Jan 4 22:13:58 CET 2021


Am 04.01.2021 um 10:33 schrieb Chris Burrows:
> Monday, 4 January 2021, Bernhard Treutwein:

>> Step;
>> WHILE ~cond DO
>>    CodeBlock;
>>    Step
>> END
>>
>
> If an action in a loop is always performed at least once, a REPEAT loop is
> usually preferable to WHILE.
>
> This then becomes:
>
> REPEAT
>    Step;
>    IF ~cond THEN CodeBlock END
> UNTIL cond

Keep in mind that this code is not equivalent in case CodeBlock can have
side effects that modify ~cond.

Then you'd have to do something like


VAR done: BOOLEAN;

done := FALSE;
REPEAT
   Step;
   IF cond THEN
     done := TRUE
   ELSE
     CodeBlock
   END
UNTIL done


But I still like it better than the WHILE solution, as it does not
duplicate the Step code.



Regards,


Michael


More information about the Oberon mailing list