On 21 May 2000, at 22:01, Frank Heckenbach wrote:
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.
Nearly. ;-) I believe item "g" actually refers to this case:
for i := 1 to 10 do for i := 11 to 20 do ;
The second for-statement is contained in the first and threatens the first for-statement's control variable, and so is illegal by item "g".
Alright, we'll take it out of the next GPC release.
I agree that it might be easier to do this than to try to understand the EP standard!
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)?
The last sentence of 6.9.2.4 says:
"It shall be a dynamic-violation if the commencement of the activation containing the program-point has not been completed (see 6.2.3.8)."
But a for-statement does not appear to be an activation. 6.2.3.2 refers to activations of "a block or a module...." A for-statement is not a block and it is not a module (it's a structured-statement).
However, on additional inspection, it appears that a goto into a loop is not legal by 6.9.1:
"A label, if any, of a statement S shall be designated as prefixing S. The label shall be permitted to occur in a goto-statement G (see 6.9.2.4) if and only if any of the following three conditions is satisfied:
(a) S contains G.
(b) S is a statement of a statement-sequence containing G.
(c) S is a statement of the statement-sequence of the compound-statement of the statement-part of a block containing G."
Item "b" appears to be the key, as the question fails items "a" and "c". Going out of a for-statement is legal:
for i := 1 to 10 do goto 1; 1: ;
...because the for-statement (part of the statement-sequence containing G) and the null-statement (S) are both part of the same statement-sequence. However, going into a for-statement is not legal:
goto 1; for i := 1 to 10 do 1: ;
...because the null-statement (S) is not part of the statement-sequence containing the goto-statement (the statement-sequence containing the goto- statement consists of the goto-statement and the for-statement but *not* the null-statement).
It took me three days of head-scratching to decide this, so I hope I am correct (this time)!
This would mean that the loop variable could practically not be used outside of the for loop.
Except in the specific case where the for-statement was exited by a goto (6.9.3.9.1, "After a for-statement is executed, other than being left by a goto-statement, the control-variable shall be undefined."). In this case, the value is valid and usable, so you wouldn't want to limit the scope to just the for-statement.
My original wrong comment was that you couldn't *assign* to the control- variable outside of the loop, although you could still read it. However, as Marco van de Voort showed, I was in error.
My head hurts! ;-)
-- Dave