J. David Bryan wrote:
On 20 May 2000, at 9:31, Russ Whitaker wrote:
So what does the Pascal specs say?
The Extended Pascal standard, section 6.9.3.9.1, says:
"After a for-statement is executed, other than being left by a goto-statement, the control-variable shall be undefined."
That's remarkably clear. :-) That means that variable "b" in the code below isn't guaranteed to be true:
In fact, i = 10 in the current GPC version (but don't rely on it :-).
procedure p; var i : integer; b : Boolean; begin for i := 1 to 10 do ; b := (i > 10); end;
The next part of the standard is a bit less clear:
"Neither a for-statement nor any procedure-and-function-declaration-part of the block that closest-contains a for-statement shall contain a statement threatening (see 6.9.4) a variable-access denoting the variable denoted by the control-variable of the for-statement."
I believe that says that you can't modify the control variable of a for- statement (a "threat" is defined in the standard as one of ten ways that a variable may be assigned a value).
Including a `for' statement (item g :-). Therefore I conclude that a `for' statement by itself is illegal in Pascal. Alright, we'll take it out of the next GPC release. Just kidding...
So this would clearly be illegal:
procedure p; var i : integer; begin for i := 1 to 10 do i := 11; end;
However, it also says that a block containing a for-statement cannot contain a threat to the control variable. In other words, this would be illegal too:
procedure p; var i : integer; begin i := 0; for i := 1 to 10 do end;
I suppose the reason is to disallow something like this:
procedure p; var i : integer; label 1, 2, 3; begin goto 1; 2: i := 11; goto 3;
1: for i := 1 to 10 do begin goto 2; 3: end
end;
...which is functionally equivalent to the second code example.
Yes, but is a goto into a loop legal at all (the last sentence of 6.9.2.4 seems to indicate not to me, but I'm not completely sure)? If it is legal, what should the following code do?
procedure p; var i : integer = 42; label 1; begin goto 1; for i := 1 to 10 do begin 1: Writeln (i) end end;
So it would seem that to comply with the EP standard, GPC must prohibit any threat (i.e., variable assignment) to a for-statement control variable anywhere within the scope of that variable, and not just within the for- statement itself.
This would mean that the loop variable could practically not be used outside of the for loop. If this is really true, I'd really favour a syntax that makes this clear (like `for i : Integer = 1 to 10 do ...'), so i has only the corresponding scope.
Frank