Pascal的正确结构语法如果然后开始结束; (在Inno设置中)

ama*_*ate 11 delphi pascal inno-setup pascalscript

自从我上次在Pascal写作以来已经有20年了.我似乎无法正确使用语言的结构元素,而不是使用begin和end的块.例如,这给我一个编译器错误"标识符预期"

procedure InitializeWizard;
begin
  Log('Initialize Wizard');
  if IsAdminLoggedOn then begin
    SetupUserGroup();
    SomeOtherProcedure();
  else begin (*Identifier Expected*)
    Log('User is not an administrator.');
    msgbox('The current user is not administrator.', mbInformation, MB_OK);
    end  
  end;
end;
Run Code Online (Sandbox Code Playgroud)

当然,如果我删除if then块和begin与它们相关的块,那么一切都OK.

有时我会得到这种语法,并且它可以正常运行,但是在嵌套end块时问题会变得恼怒.

解决这个问题还不够.我想更好地了解如何使用这些块.我显然错过了一个概念.来自C++或C#的东西可能正在从我的另一部分中悄悄进入并弄乱我的理解.我已经阅读了一些关于它的文章,我认为我理解它然后我没有.

Ken*_*ite 30

你必须在同一级别匹配每begin一个end,比如

if Condition then
begin
  DoSomething;
end
else
begin
  DoADifferentThing;
end;
Run Code Online (Sandbox Code Playgroud)

如果您愿意,可以缩短使用的行数而不影响展示位置.(但是,当您第一次习惯语法时,上述内容可能会更容易.)

if Condition then begin
  DoSomething
end else begin
  DoADifferentThing;
end;
Run Code Online (Sandbox Code Playgroud)

如果您正在执行单个语句,则它begin..end是可选的.请注意,第一个条件不包含终止;,因为您尚未结束语句:

if Condition then
  DoSomething
else
  DoADifferentThing;
Run Code Online (Sandbox Code Playgroud)

分号在块的最后一个语句中是可选的(尽管我通常包括它,即使它是可选的,以避免在添加行时忘记将来的问题而忘记同时更新前一行).

if Condition then
begin
  DoSomething;            // Semicolon required here
  DoSomethingElse;        // Semicolon optional here
end;                      // Semicolon required here unless the
                          // next line is another 'end'.
Run Code Online (Sandbox Code Playgroud)

您还可以组合单个和多个语句块:

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

if Condition then
  DoSomething
else
begin
  DoADifferentThing;
  DoAnotherDifferentThing;
end;
Run Code Online (Sandbox Code Playgroud)

正确使用您的代码将是:

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;
Run Code Online (Sandbox Code Playgroud)

  • @amalgamate:如果您不使用`begin`,则不需要`end`。请参阅我上面的第三个代码示例。 (2认同)