加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 为什么“单一语句”块不需要使用分号?

发布时间:2020-12-15 10:15:02 所属栏目:大数据 来源:网络整理
导读:我通常是一个C#程序员,去德尔福充满了“有趣”的发现。最令我迷惑的一个是德尔福的单一声明。 示例C#块 if(x) Foo();else Bar(); 示例Delphi块: if x then Foo() //note missing semicolonelse Bar(); 他们的目的是要求分号不在那里?有追溯到帕斯卡的历史
我通常是一个C#程序员,去德尔福充满了“有趣”的发现。最令我迷惑的一个是德尔福的单一声明。

示例C#块

if(x) 
  Foo();
else
  Bar();

示例Delphi块:

if x then
  Foo() //note missing semicolon
else
  Bar();

他们的目的是要求分号不在那里?有追溯到帕斯卡的历史原因吗?

解决方法

Pascal和C及其衍生物中的分号之间存在差异。

>在C中,分号是一个语句终止符。
>在Pascal中,分号是一个语句分隔符。

Wikipedia解释了这个的含义:

This difference manifests itself primarily in two situations:

  • there can never be a semicolon directly before else in Pascal
    whereas it is mandatory in C (unless a block statement is used)
  • the last statement before an end is not required to be followed by
    a semicolon

A superfluous semicolon can be put on the last line before end,thereby formally inserting an empty statement.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读