[Oberon] [Component Pascal]

Claudio Nieder private at claudio.ch
Sat Feb 16 22:11:32 CET 2013


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







More information about the Oberon mailing list