[Oberon] FOR loop variable

Paul Reed paulreed at paddedcell.com
Tue Mar 14 23:20:59 CET 2023


Hi Ala'a,

On 2023-03-14 20:33, Ala'a Mohammad wrote:
> I need help to understand the need to declare FOR loop stepping's
> variable. (i.e x in FOR x := .....). I do not intend to change the
> language, but The question arises since this variable always (in my
> short exposure to Oberon-2) is scoped within the FOR loop body. And
> was trying to answer the question:
> Is it good design to have FOR implicitly declare its variable (It is
> not a big issue to declare VARiables), or Is it from the point of
> uniformity to have all variables declared with VAR?

Thanks for a great question!

FOR in the language report is defined to be equivalent to an assignment 
of the control variable (and the limit, in the case of Oberon-2; see 
below) followed by a WHILE loop containing the statement sequence and a 
fixed increment/decrement of the control variable.

Therefore FOR *does not* introduce another scope, unlike in some other 
languages.

And at first sight it seems there's nothing special about the control 
variable: as you say, for uniformity, it may be declared (and re-used) 
just like any other variable (at the beginning of the scope, or 
globally, which makes its type relatively easy to find).

However, that the statement sequence must be executed "a fixed number of 
times", so implementations will typically make the control variable 
read-only (preventing assignments to it) inside the FOR. So it's a *bit* 
special. :)

Some implementations might not necessarily protect the "fixed number of 
times", for example if they don't assign the limit expression to a 
temporary variable at the start of the loop. YMMV. Literally. :)

HTH,
Paul


More information about the Oberon mailing list