HomeDelphiDelphi Error – W1037 FOR-Loop variable ‘%s’ may be undefined after loop

Delphi Error – W1037 FOR-Loop variable ‘%s’ may be undefined after loop

Delphi Compiler Error

W1037 FOR-Loop variable ‘%s’ may be undefined after loop

Reason for the Error & Solution

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.

Share:

Leave a Reply

You May Also Like

Delphi Compiler Error X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...