Proper structure syntax for Delphi/Pascal if then begin end and ;

You have to match every begin with an end at the same level, like

if Condition then
begin
  DoSomething;
end
else
begin
  DoADifferentThing;
end;

You can shorten the number of lines used without affecting the placement, if you prefer. (The above might be easier when you’re first getting used to the syntax, though.)

if Condition then begin
  DoSomething
end else begin
  DoADifferentThing;
end;

If you’re executing a single statement, the begin..end are optional. Note that the first condition does not contain a terminating ;, as you’re not yet ending the statement:

if Condition then
  DoSomething
else
  DoADifferentThing;

The semicolon is optional at the last statement in a block (although I typically include it even when it’s optional, to avoid future issues when you add a line and forget to update the preceding line at the same time).

if Condition then
begin
  DoSomething;            // Semicolon required here
  DoSomethingElse;        // Semicolon optional here
end;                      // Semicolon required here unless the
                          // next line is another 'end'.

You can combine single and multiple statement blocks as well:

if Condition then
begin
  DoSomething;
  DoSomethingElse;
end
else
  DoADifferentThing;

if Condition then
  DoSomething
else
begin
  DoADifferentThing;
  DoAnotherDifferentThing;
end;

The correct use for your code would be:

procedure InitializeWizard;
begin
  Log('Initialize Wizard');
  if IsAdminLoggedOn then 
  begin
    SetupUserGroup();
    SomeOtherProcedure();
  end 
  else 
  begin 
    Log('User is not an administrator.');
    msgbox('The current user is not administrator.', mbInformation, MB_OK);
  end;
end;

Leave a Comment