Is this legal? FOR ix := 0 TO 2 DO INC( ix ) END; -- Aubrey McIntosh, Ph.D. 211 E. 5th St. Morris MN 56267 (512)-348-7401
On Saturday 16 February 2013 14:49:38 Aubrey.McIntosh@alumni.utexas.net wrote:
Is this legal?
FOR ix := 0 TO 2 DO INC( ix ) END;
legal and senseless -- Met vriendelijke groeten, Jan Verhoeven http://www.verhoeven272.nl
Hi Yes it's legal. However the runtime behaviour depends on how clever the compiler is. The book "Programming in Oberon" says "The for statement has the advantage of guaranteed termination." So for example a stupid compiler translates the following code into a program that runs forever: FOR i := 1 TO 2 DO DEC(i) END. A more clever compiler translates FOR i := m TO n-1 DO (* some processing *) END; into temp := m; WHILE temp < n DO i:= temp; (* some processing *) INC(temp) END; Jörg On 16.02.2013, at 14:49, <Aubrey.McIntosh@Alumni.UTexas.Net> wrote:
Is this legal?
FOR ix := 0 TO 2 DO INC( ix ) END;
-- Aubrey McIntosh, Ph.D. 211 E. 5th St. Morris MN 56267 (512)-348-7401
-- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
Hi,
FOR ix := 0 TO 2 DO INC( ix ) END;
legal and senseless
And I wonder if the implementation conforms to the specification. I found a definition of Component Pascal at http://www.oberon.ch/pdf/CP-Lang.pdf which states The statement FOR v := beg TO end BY step DO statements END is equivalent to temp := end; v := beg; IF step > 0 THEN WHILE v <= temp DO statements; INC(v, step) END ELSE WHILE v >= temp DO statements; INC(v, step) END END But I wouldn't wonder if the actual implementation would more resemble to temp := end; temp2 := beg; IF step > 0 THEN WHILE temp2 <= temp DO v:=temp2; statements; INC(temp2, step) END ELSE WHILE temp2 >= temp DO v:=temp2; statements; INC(temp2, step) END END Thus in the special case where the statement modifies the control variable of the FOR loop the outcome will differ depending on how the compiler author actually did implement it. In "Programmin in Modula-2" Wirth wrote regarding the control variable "Its value should not be changed by the statement sequence" which hints that if you nevertheless do it, then you shouldn't complain if the outcome is not as you expect. Wirth avoided this issue then in Oberon by dropping the handy FOR statement, and if you care about reproducibility of your programs result, you should write all cases where you need to modify the control variable not as a FOR but as a WHILE statement. claudio -- Claudio Nieder, Talweg 6, CH-8610 Uster, Tel +4179 357 6743, www.claudio.ch
I remember reading this. So I was surprised when my nonsensical code snipped compiled. Previously, I was surprised when the code did not work correctly. On Sat, Feb 16, 2013 at 3:11 PM, Claudio Nieder <private@claudio.ch> wrote:
In "Programmin in Modula-2" Wirth wrote regarding the control variable "Its value should not be changed by the statement sequence" which hints that if you nevertheless do it, then you shouldn't complain if the outcome is not as you expect.
claudio -- Claudio Nieder, Talweg 6, CH-8610 Uster, Tel +4179 357 6743, www.claudio.ch
-- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
-- Aubrey McIntosh, Ph.D. 211 E. 5th St. Morris MN 56267 (512)-348-7401
Claudio Nieder wrote:-
Thus in the special case where the statement modifies the control variable of the FOR loop the outcome will differ depending on how the compiler author actually did implement it. Being compiler dependant is completely unacceptable.
In "Programmin in Modula-2" Wirth wrote regarding the control variable "Its value should not be changed by the statement sequence".. Because it's such nice syntax-sugar, couldn't the compiler check that the control variable is <read only>. Well, if you START adding such 'band-aids' you end up with a mess. And KISS *IS THE* axiom.
== Chris Glur
On 02/16/13 22:11, Claudio Nieder wrote:
The statement FOR v := beg TO end BY step DO statements END
is equivalent to
temp := end; v := beg; IF step > 0 THEN WHILE v <= temp DO statements; INC(v, step) END ELSE WHILE v >= temp DO statements; INC(v, step) END END
Yes, this is also the definition in the Oberon-2 language report.
But I wouldn't wonder if the actual implementation would more resemble to
temp := end; temp2 := beg; IF step > 0 THEN WHILE temp2 <= temp DO v:=temp2; statements; INC(temp2, step) END ELSE WHILE temp2 >= temp DO v:=temp2; statements; INC(temp2, step) END END
In this case the compiler would not conform to the language specification. The behaviour of the FOR loop can, however, really differ depending on the compiler in the following case: VAR v: INTEGER; ... FOR v := 1 TO MAX(INTEGER) DO ... END; The program may either loop forever or terminate due to a recognized overflow condition in INC(v,1). However, this does not mean that the FOR loop is ill-defined. The language definition simply does not specify if and when arithmetic overflows have to be checked. Thus, the same situation can occur with other loops constructs. Richard
participants (6)
-
Aubrey.McIntosh@Alumni.UTexas.Net -
Claudio Nieder -
eas lab -
Jan Verhoeven -
Jörg Straube -
Richard Hable