Hi, I'm a beginner in Oberon-2 who had recently picked it up (learning it through practice). and really love the simplicity of the language. 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? forgive any ignorance on my part Thanks for your time. Ala'a Regards,
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
Hi Paul, Thanks alot for your time and appreciate the detailed explanation. Regards, On Wed, Mar 15, 2023 at 2:21 AM Paul Reed <paulreed@paddedcell.com> wrote:
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 -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
participants (2)
-
Ala'a Mohammad -
Paul Reed