Search code examples
lazarusfreepascal

Syntax error, ";" expected but "ELSE" found - Error with IF-ELSE condition - FreePascal


I dont understand why my code doesn't compile, can anyone help me about this. Any kind of comment is helpful

this is the code:

procedure TForm1.Button2Click(Sender: TObject);
var
  days : integer;
begin
  if(Edit2.Text <> '') then
    days:= StrToInt(Edit2.Text);
    DDate.Minus(days);
    Edit1.Text := DDate.GetDate
  else
    ShowMessage('The field is required');
end;

Solution

  • If you need two or more statements for true_statement or false_statement, then the group of statements must be placed within a begin ... end Block

    procedure TForm1.Button2Click(Sender: TObject);
    var
      days: integer;
    begin
      if(Edit2.Text <> '') then
       begin
        days := StrToInt(Edit2.Text);
        DDate.Resta(days);
        Edit1.Text := DDate.GetDate;
       end
      else
        ShowMessage('The field is required');
    end; 
    

    Source: http://wiki.freepascal.org/Else