If you follow StrawberryFrog
's advise, I'm sure it'll work
fine in Delphi
, but many traditional pascal compilers
will consider that a syntax error.
Pascal's semicolon rules are not any more complex than C.
They're just different.
In C, the semicolon is a statement terminator, and sometimes is itself a null statement, and so you can have as many as you want in a row with no actual code between.
In fact, if you want to put a label before a close brace,
you must put a semicolon after the label to make a null
statement. (The label is a goto target, so you need a
statement there to be the target.)
In pascal, the semicolon is a statement separator, and only goes between
statements. In some (traditional) pascal compilers, there
is no null statement, and so even one extra semicolon is actually a syntax error, including before an end. Many pascal compilers will allow this, however, which
makes them a lot easier to deal with.
I'll steal the examples from above, and translate to C,
to show the difference:
- Single statement if-then-else:
This is the one that confuses most beginning students,
and is the example left out of StrawberryFrog's wu above.
printf("N is no");
if (n == 0) // parens optional in pascal, required in C
printf("w"); // semicolon required in C, error in pascal!!!
printf(" zero."); // semicolon added
- if-then-else using blocks in pascal
if n = 0 then // parens optional -- left out
ShowMessage('N is now zero');
Result := 1; // some compilers will take this
end // never use a semicolon before else!
- if-then-else using blocks in C
if (n == 0) // parens required
ShowMessage("N is now zero');
Result = 1; // required
} // semicolon not needed after a brace!
ShowMessage("N is not zero'); // required
Actually, in the C example, putting a semicolon after the close brace would
put a null statement between the then statement and the else keyword,
which would be a syntax error, just like in pascal, but for a different reason.
I think StrawberryFrog's point is that if you always use begin and end,
and your compiler takes ; as a null statement, then it is
about the same as C.
Personally, I find the single statement version of if-then-else more elegant when applicable, and the inconsistency of not putting a ; after every statement
is a wart
in the elegance
. It's a bigger wart if you
have a compiler that also doesn't like ; immediately
before end statements, but then at least it is consistent