Trying again
to demonstrate disadventages of the new implementation of
for... to loop, I've stumbled on an error.
Loop in the following code executes 3 times instead of expected 4
Code: Select all
k:=2;
j:=0;
for i:=k shr 1 to k shl 1 do
begin
j:=j+2;
if j=8 then continue;
end;
The loop should run from i=1 to i=4 giving j=8. Unfortunately, it ends with j=6. The error is partly due to wrong implementation, because the condition
intial counter value<=final value is being checked inside the loop, instead of just once at the beginning. As the compiler uses the same internal variable for initial counter value and calculations inside the loop, the result is as shown.
What I intended to do before stumbling on this error, was to show an unpleasant property of the new implementation. The following loop will run forever
Code: Select all
for i:=1 to 4 do
begin
if i=4 then continue;
end;
because the
continue statement
increases the counter
without checking if final value was reached (exactly opposite to the explanation in help:
continue statement in for loop moves program counter to the line with keyword for; it does not change the loop counter
)