[…] And I think that your solution is not guaranteed to be working.
Well, ISO standard 7185 “Standard Pascal” says
[…] After a for‑statement is executed, other than being left by a goto‑statement, the control-variable shall be undefined. […]
I don’t think this means “
undefine (verb) counter variable”, but the specific value is not defined
by the
for-statement. If you define (assign) a value
outside the
for-loop, it certainly remains defined. The
for-loop
does not invalidate any previous value the counter-variable held.
However, as 440bx already remarked, the counter variable may be stored as a processor register, say
ecx. Only a
goto will ensure this volatile value will be written back into the respective variable’s memory location.
Meanwhile, it’s FPC we’re talking about. The
documentation says it pretty clearly:
- The value of the loop variable is undefined after a loop has completed or if a loop is not executed at all. However, if the loop was terminated prematurely with an exception or a break or goto statement, the loop variable retains the value it had when the loop was exited.