Delphi/Pascal 的正确结构语法 if then begin end and ; [英] Proper structure syntax for Delphi/Pascal if then begin end and ;

查看:57
本文介绍了Delphi/Pascal 的正确结构语法 if then begin end and ;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

距我上次不得不用 Pascal 编写已经大约 20 年了.我似乎无法在使用 beginend 嵌套 if then 块的地方正确使用语言的结构元素.例如,这让我得到一个编译器错误 "Identifier Expected".

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;

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

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

有时我理解这种语法是正确的,它运行正常,但是在嵌套 if then 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.

推荐答案

你必须将每个 begin 与同一级别的 end 匹配,例如

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;

如果您正在执行单个语句,begin..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 的正确结构语法 if then begin end and ;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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