Pascal的正确结构语法如果然后开始结束; (在Inno设置中) [英] Proper structure syntax for Pascal if then begin end and ; (in Inno Setup)

查看:174
本文介绍了Pascal的正确结构语法如果然后开始结束; (在Inno设置中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我最后一次写在帕斯卡尔以来已经有二十年了。我似乎不能使用正确嵌套的语言的结构元素,而不是使用开始和结束的块。例如,这会给我一个编译器错误需要标识符

  procedure InitializeWizard; 
begin
日志('初始化向导');
如果IsAdminLoggedOn然后开始
SetupUserGroup();
SomeOtherProcedure();
else begin(* Identifier Expected *)
日志('用户不是管理员');
msgbox('当前用户不是管理员',mbInformation,MB_OK);
end
end;
结束

当然,如果我删除,那么块和开始结束与它们相关的块,那么一切都可以。



有时我得到这种语法权利,它的作用是可以的,但是当嵌套(如果另外)块时,问题变得恼怒。



在这里解决问题是不够的。我想更好地了解如何使用这些块。我显然错失了一个概念。 C ++或C#中的某些东西可能从我脑海中的另一部分爬行,弄乱了我的理解。我读了几篇关于它的文章,而且我觉得我明白了,然后我就不了解。

解决方案

你有在每个开始结束在同一级别匹配,如

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

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

 如果条件然后开始
DoSomething
end else begin
DoADifferentThing;
结束

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

  if Condition then 
DoSomething
else
DoADifferentThing;

分号在块中的最后一个语句是可选的(虽然我通常包括它,即使它是可选的,以避免未来的问题,当你添加一行,并忘记在同一时间更新前一行)。

  if Condition then 
begin
DoSomething; //这里需要分号
DoSomethingElse; //分号可选这里
end; //分号需要这里,除非
//下一行是另一个结束。

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

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

如果条件然后
DoSomething
else
begin
DoADifferentThing;
DoAnotherDifferentThing;
结束

正确使用您的代码将是:

  procedure InitializeWizard; 
begin
日志('初始化向导');
如果IsAdminLoggedOn然后
begin
SetupUserGroup();
SomeOtherProcedure();
end
else
begin
日志('用户不是管理员');
msgbox('当前用户不是管理员',mbInformation,MB_OK);
结束
结束


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 than 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.

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.

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;

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;

这篇关于Pascal的正确结构语法如果然后开始结束; (在Inno设置中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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