我通常是一个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 semicolonA superfluous semicolon can be put on the last line before end,thereby formally inserting an empty statement.