W1037 FOR-Loop variable '%s' may be undefined after loop (Delphi)

From RAD Studio
Jump to: navigation, search

Go Up to Error and Warning Messages (Delphi)

This warning is issued if the value of a for loop control variable is used after the loop.

You can only rely on the final value of a for loop control variable if the loop is left with a goto or exit statement.

The purpose of this restriction is to enable the compiler to generate efficient code for the for loop.

program Produce;
(*$WARNINGS ON*)
 
function Test1: Integer;
var
  I: Byte;
begin
  Result := 0;
  for I := 0 to 255 do
    Inc(Result);
  WriteLn(I); // undefined
end;
 
begin
  Test1;
end.

In the example, the control variable is used implicitly after the loop, so that it may be undefined - hence the warning.