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

前端之家收集整理的这篇文章主要介绍了delphi – 为什么“单一语句”块不需要使用分号?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我通常是一个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.

原文链接:https://www.f2er.com/delphi/103332.html

猜你在找的Delphi相关文章