The for
loop is well defined in terms of a while
loop, confer ISO 7185 “Standard Pascal”, quote:
Apart from the restrictions imposed by these requirements, the for-statement
for v := e1 to e2 do body
shall be equivalent to
begin
temp1 := e1;
temp2 := e2;
if temp1 <= temp2 then
begin
v := temp1;
body;
while v <> temp2 do
begin
v := succ(v);
body
end
end
end
[…]
where temp1
and temp2
denote auxiliary variables that the program does not otherwise contain, and that possess the type possessed by the variable v
[…]
The important thing from this expanded piece of code is, as linuxfan says Reinstate Monica already noted, that in Pascal the limits of a for
loop are only evaluated once. Furthermore, the control-variable is only assigned a value if there was indeed at least one iteration.
However, usually it’s sufficient to transform a for
loop as Andreas Rejbrand already suggested, although it is technically not the same.