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

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error E2313 Attribute – Known attribute cannot specify properties Reason for the Error & Solution No further information...
Delphi Compiler Error E2379 Virtual methods not allowed in record types Reason for the Error & Solution No further information...
Rodrigo , one of the long time Delphi Developer has been working on one of his personal project “Delphi IDE...