HomeDelphiDelphi Error – E2097 BREAK or CONTINUE outside of loop

Delphi Error – E2097 BREAK or CONTINUE outside of loop

Delphi Compiler Error

E2097 BREAK or CONTINUE outside of loop

Reason for the Error & Solution

The compiler has found a BREAK or CONTINUE statement which is not contained inside a WHILE or REPEAT loop. These two constructs are only legal in loops.

program Produce;

  procedure Error;
    var i : Integer;
  begin
    i := 0;
    while i < 100 do
      INC(i);
      if odd(i) then begin
        INC(i);
  continue;
      end;
  end;

begin
end.

The example above shows how a continue statement could seem to be included in the body of a looping construct but, due to the compound-statement nature of The Delphi language, it really is not.

program Solve;

  procedure Error;
    var i : Integer;
  begin
    i := 0;
    while i < 100 do begin
      INC(i);
      if odd(i) then begin
        INC(i);
  continue;
      end;
    end;
  end;

begin
end.

Often times it is a simple matter to create compound statement out of the looping construct to ensure that your CONTINUE or BREAK statements are included.

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