19

I'm usually a C# programmer and going to Delphi has been full of "interesting" discoveries. The one that baffles me the most is single statements in Delphi.

Example C# block

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

Example Delphi block:

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

What exactly was their purpose for requiring that semi-colon to not be there? Is there a historical reason dating back to Pascal?

Earlz
  • 62,085
  • 98
  • 303
  • 499
  • 14
    Nice to see someone moving from C# to Delphi! – Larry Lustig Sep 27 '11 at 19:30
  • 4
    As a life-long Delphi programmer, the first snippet hurts my eyes. – Andreas Rejbrand Sep 27 '11 at 19:34
  • FYI: If you wrap foo() in a Begin/End block the End doesn't need a semi-colon but the statements in the block all have follow the standard rules concerning the use of semi-colon. – Vivian Mills Sep 27 '11 at 19:34
  • 3
    @Ryan: Well, the statement prior to the `end` doesn't require a semicolon either. – Andreas Rejbrand Sep 27 '11 at 19:35
  • @Andreas Both hurt my eyes, `if condition then begin` all the way for me!! – David Heffernan Sep 27 '11 at 19:37
  • Another interesting fact, the last statement of a begin end block does not need a semi-colon. While most would consider it bad practice not to have the semi-colon, it is still valid code to the compiler. This is due to the nature of semi-colons in Pascal as pointed out by David Heffernan. – Vivian Mills Sep 27 '11 at 19:38
  • @Andreas Rejbrand: Yeah I was getting to that. :) – Vivian Mills Sep 27 '11 at 19:38
  • @David: That's where we disagree. I sometimes frown upon your 'unnecessary' `begin` and `end` parts! – Andreas Rejbrand Sep 27 '11 at 19:39
  • 2
    @Larry I'm being forced to move :P (old work project is written in Delphi. Getting it fixed and moving it over the C# :) Also, it's Delphi 7 to make it worse ) – Earlz Sep 27 '11 at 19:40
  • It's to avoid ambiguity with `case else` and `if then else`. – Johan Sep 27 '11 at 20:28
  • 2
    @AndreasRejbrand - I used to be a hard-core proponent of the "avoid unnecessary brackets" school of thought. After years of "experience" I've found out that using them will avoid future headaches when a stupider, older version of yourself modifies your "else" block by adding an extra instruction thinking it will "stick" to the previous statement. In Delphi this is not much an issue, to be fair, but since I also work quite a bit with C and Java, it has become a habit. – Leonardo Herrera Sep 28 '11 at 14:48

2 Answers2

32

There is a difference between semi-colons in Pascal and in C and their derivatives.

  • In C the semi-colon is a statement terminator.
  • In Pascal the semi-colon is a statement separator.

Wikipedia explains the implications of this:

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • And so `;;;;;` is valid code in both languages. In Pascal one writes unneeded semicolons more often than being aware of. But not writing them is impractical, simply because sooner or later code is added or rearranged and a **separating** semicolon must be added anyway. – AmigoJack Feb 07 '22 at 23:34
30

The real reason ; is not allowed in front of a if-then else is to avoid ambiguity with its lesser known cousin, the case-of else.

Observe the following snippet:

case enum1 of
  male: writeln('hallo');
  female: if a=1 then writeln('oops');  <<-- watch this space.
  else writeln('neither')
end; 

Because there is a ; after the 'oops' line, the else belongs to the case statement and not the if.

If you leave out the ; then the else belongs to the if a=1.

That's why a ; is not allowed in front of an if else.

Personally having worked in Pascal for some 20-odd years, I still put ; in front of else, because I put ; in C-style. And the compiler still bugs me, you'd think the compiler would have learned by now.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Johan
  • 74,508
  • 24
  • 191
  • 319
  • 3
    +1 This is one of a number of reasons why I only use compound statements. – David Heffernan Sep 27 '11 at 20:25
  • 4
    @DavidHeffernan, Yep due to hard knocks I've learned that too. I can write `begin` faster than typing a `{` – Johan Sep 27 '11 at 20:31
  • 2
    I once experimented with using pragma statements to make use of begin end in c. It worked ok, but is not used anymore. But today my c code style is more or less a pascal look-alike. And you really can't overuse begin end. Makes code easier to maintain and understand. – LU RD Sep 27 '11 at 20:38
  • 2
    I put the begin on the same line as the if, and the `end else if` or `end else begin` all on the same line. Looks weird for the first week but 15 years on it's fine. Modula-2 got it right. Sigh. – David Heffernan Sep 27 '11 at 20:40
  • 2
    I actually invested in Modula-2 for DOS about two weeks before I saw TP 2.0. It was a wow moment and I never looked back since then. The modula binder is still in the book shelf somewhere. – LU RD Sep 27 '11 at 20:54
  • Strings as case labels? – Andreas Rejbrand Jan 01 '18 at 00:09
  • @AndreasRejbrand, I'm working on it. Pretty soon the political pressure from SO will force the powers that be to add this indispensable feature. – Johan Jan 02 '18 at 17:25
  • I think the reasoning in this answer (from 2011, I know :-)) is much too complicated and even misleading. The reason why a `;` isn't allowed here is because between `if` and `else` only *one* statement is allowed, while `Foo;` are *two* statements. – Uli Gerhardt Jan 02 '18 at 18:11
  • 1
    _The real reason ; is not allowed in front of a if-then else is to avoid ambiguity with its lesser known cousin, the case-of else._ Untrue! The original PASCAL syntax is case-of-otherwise - it's only Turbo Pascal that made it into case-of-else. The _real_ reason is - as David Heffernan stated - that a semicolon in PASCAL is a statement _seperator_, ie. it _seperates_ TWO _statements_. "else" is not a _statement_ and thus no semicolon is allowed. Likewise a semicolon before END is not required (as END is not a _statement_), but since empty statements are allowed, you can put a semicolon there. – HeartWare Jan 02 '18 at 19:08
  • The syntax for an IF statement is "IF THEN ELSE ". As you can see, ELSE is not a statement. – HeartWare Jan 02 '18 at 19:08