Proper structure syntax for Pascal if then begin end and ;

僤鯓⒐⒋嵵緔 提交于 2019-11-26 20:47:49
Ken White

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!