Delphi – E2081 Assignment to FOR-Loop variable ‘%s’

Inside the for loop, it is prohibited to set a value to the for loop control variable.

Use a break or goto statement if the goal is to exit the loop early.

program Produce;

var
  I: Integer;
  A: array [0..99] of Integer;
begin
  for I := 0 to 99 do begin
    if A[I] = 42 then
      I := 99;
  end;
end.

The programmer assumed that setting 99 to I would lead the programme to exit the loop in this scenario.

program Solve;

var
  I: Integer;
  A: array [0..99] of Integer;
begin
  for I := 0 to 99 do begin
    if A[I] = 42 then
      Break;
  end;
end.

A break statement is a more elegant approach to end a for loop.

%d bloggers like this: