Understanding Oberon07's WHILE loops
Hello, is the following understanding correct, that given conditions c1/c2 and statement sequences s1 and s2: WHILE c1 DO s1 ELSIF c2 DO s2 END; is equivalent to this code: LOOP IF c1 THEN s1 ELSIF c2 THEN s2 ELSE EXIT END END; The reason I am unsure is the wording in Wirth's "Differences between Revised Oberon and Oberon" where it says: "As long as any of the Boolean expressions yields TRUE, the corresponding statement sequence is executed." In my understanding only at most one statement sequence per iteration is executed and that is the first one for which the condition is true. Greetings, Chris
-----Original Message----- From: chris [mailto:chris@gcjd.org] Sent: Saturday, 7 March 2015 7:56 AM To: oberon@lists.inf.ethz.ch Subject: [Oberon] Understanding Oberon07's WHILE loops
Hello,
is the following understanding correct, that given conditions c1/c2 and statement sequences s1 and s2:
WHILE c1 DO s1 ELSIF c2 DO s2 END;
is equivalent to this code:
LOOP IF c1 THEN s1 ELSIF c2 THEN s2 ELSE EXIT END END;
The reason I am unsure is the wording in Wirth's "Differences between Revised Oberon and Oberon" where it says: "As long as any of the Boolean expressions yields TRUE, the corresponding statement sequence is executed."
In my understanding only at most one statement sequence per iteration is executed and that is the first one for which the condition is true.
Yes - your understanding is correct. However I find it easier to understand the operation of the Oberon-07 WHILE loop by rewriting your LOOP as a conventional WHILE loop (also still legal in Oberon-07): C0 := FALSE; WHILE C0 DO IF C1 THEN S1 ELSIF c2 THEN S2 ELSE C0 := FALSE END END; You can now see that the Oberon-07 extended WHILE loop uses the keyword DO instead of THEN and automatically takes care of the housekeeping associated with the additional control variable C0. As an example of the extended WHILE: i := 0; WHILE i < 3 DO INC(i); Out.Char("A"); Out.Int(i, 3); Out.Ln; ELSIF i < 6 DO INC(i); Out.Char("B"); Out.Int(i, 3); Out.Ln; END Produces the following output: A 1 A 2 A 3 B 4 B 5 B 6 Regards, Chris Burrows CFB Software Astrobe: Oberon-07 for ARM Cortex-M3 and M4 Microcontrollers http://www.astrobe.com
On Sat, 7 Mar 2015 10:01:41 +1030, Chris Burrows wrote:
Yes - your understanding is correct. However I find it easier to understand the operation of the Oberon-07 WHILE loop by rewriting your LOOP as a conventional WHILE loop (also still legal in Oberon-07):
Thanks for clarification. I am still not sure if I like these loops. The reuse of the keyword ELSIF is irritating. Probably ELSEWHILE would have been clearer. Greetings, Chris
participants (2)
-
chris -
Chris Burrows