HomeDelphiDelphi Error – H2135 FOR or WHILE loop executes zero times – deleted

Delphi Error – H2135 FOR or WHILE loop executes zero times – deleted

Delphi Compiler Error

H2135 FOR or WHILE loop executes zero times – deleted

Reason for the Error & Solution

The compiler has determined that the specified looping structure will not ever execute, so as an optimization it will remove it. Example:

program Produce;
(*$HINTS ON*)

  var
    i : Integer;

begin
  i := 0;
  WHILE FALSE AND (i < 100) DO
    INC(i);
end.

The compiler determines that ‘FALSE AND (i < 100)’ always evaluates to FALSE, and then easily determines that the loop will not be executed.

program Solve;
(*$HINTS ON*)

  var
    i : Integer;

begin
  i := 0;
  WHILE i < 100 DO
    INC(i);
end.

The solution to this hint is to check the boolean expression used to control while statements is not always FALSE. In the for loops you should make sure that (upper bound – lower bound) >= 1.

You may see this warning if a FOR loop increments its control variable from a value within the range of Longint to a value outside the range of Longint. For example:

var I: Cardinal;
begin
  For I := 0 to $FFFFFFFF do
...

This results from a limitation in the compiler which you can work around by replacing the FOR loop with a WHILE loop.

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...