如果正确,则使用Delphi / Pascal的结构语法,然后开始end和; [英] Proper structure syntax for Delphi/Pascal if then begin end and ;

查看:65
本文介绍了如果正确,则使用Delphi / Pascal的结构语法,然后开始end和;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我上次不得不在Pascal写作以来已有20年了。我似乎无法在如果使用开始嵌套的情况下正确使用语言的结构元素结束。例如,这使我得到了编译器错误期望的标识符

It has been around 20 years since I last had to write in Pascal. I can't seem to use the structure elements of the language correctly where I am nesting if then blocks using begin and end. For example this gets me an Compiler Error "Identifier Expected".

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;

当然,如果我删除了,那么块和与它们相关联的结束块,那么一切正常。

Of course if I remove the if then block and the begin end blocks associated with them then everything is OK.

有时我会得到这种语法正确,效果很好,但是嵌套 if else 块时,问题变得更加恼火。

Sometimes I get it this kind of syntax right and it works out OK, but the problems become exasperated when nesting the if then else blocks.

在这里解决问题还不够。我想更好地了解如何使用这些块。我显然错过了一个概念。来自C ++或C#的某些东西可能正在从我的另一部分潜入,并弄乱了我的理解。我已经阅读了几篇有关它的文章,并且我想我也理解了,然后我却听不懂。

Solving the problem is not enough here. I want to have a better understanding how to use these blocks. I am clearly missing a concept. Something from C++ or C# is probably creeping in from another part of my mind and messing up my understanding. I have read a few articles about it, and well I think I understand it and then I don't.

推荐答案

以便将每个开始与一个结束在相同级别进行匹配,例如

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;

如果您正在执行单个语句,则开始..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;

这篇关于如果正确,则使用Delphi / Pascal的结构语法,然后开始end和;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆