E2153 ';' not allowed before 'ELSE' (Delphi)

From RAD Studio
Jump to: navigation, search

Go Up to Error and Warning Messages (Delphi) Index

You have placed a ';' directly before an ELSE in an IF-ELSE statement. The reason for this is that the ';' is treated as a statement separator, not a statement terminator - IF-ELSE is one statement, a ';' cannot appear in the middle (unless you use compound statements).


program Produce;

  var
    b : Integer;

begin
  if b = 10 then
    b := 0;
  else
    b := 10;
end.

The Delphi language does not allow a ';' to be placed directly before an ELSE statement. In the code above, an error will be flagged because of this fact.


program Solve;

  var
    b : Integer;

begin
  if b = 10 then
    b := 0
  else
    b := 10;

  if b = 10 then begin
    b := 0;
  end
  else begin
    b := 10;
  end;

end.

There are two easy solutions to this problem. The first is to remove the offending ';'. The second is to create compound statements for each part of the IF-ELSE. If $HINTS are turned on, you will receive a hint about the value assigned to 'b' is never used.