There's no reason to use Pascal (see, e.g., why Pascal is not my favorite programming language or why Pascal sucks), but that doesn't mean it's not worth knowing. Probably the least understood feature of Pascal is the use of the semicolon (that's `;', to any native English-speakers who may have wandered in). My University professor was unable to explain this at all (not too surprising; he was unable to explain a lot of other things).

Most of the confusion stems from people thinking semicolons in Pascal are like semicolons in C. They're not, and using them as if they were will insert lots of empty statements into your code, and the occasional syntax error. C uses semicolons as terminators -- so you put one at the end of every statement.

Pascal uses semicolons as separators -- so you put one between statements. This is all you need to know to use them correctly. Should I put in a semicolon? Only if the next thing is a statement!

Some examples:

No semicolon before end:
end is not a statement (it's a part of the block structure), so you don't put a semicolon before it:
if (n = 0) then begin
  writeln('N is now zero');
  func := 1
end
Adding the (redundant) semicolon before the end just adds an empty statement before it, which is legal, so the compiler doesn't complain. But don't do it: not understanding the rule will lead to trouble!
No semicolon before else:
else is part of the if ... then ... else ... statement, so you cannot separate it out!
write('N is no');
if (n = 0) then
  write('w')
else
  write('t');
writeln(' zero.')
Adding the semicolon in anyway ends the if statement (turning it into a if ... then ... statement. If you do that, the next statement is else ..., which is illegal, and the compiler gives an error message. Not understanding this error message is the #1 reason why people think Pascal has complicated rules for semicolon placement.
begin ... end blocks are statements
So after an end, you continue to put a semicolon if you need to separate it from the next statement. This is in marked contrast to C, where a { ... } block doesn't take a semicolon.
That's it!

Oh, one more thing: the last end in a program isn't followed by any statement, so it doesn't take a semicolon after it. But it's the end of the program, so it take a full stop: end..

The rules for semicolon placement in Pascal are no longer than those for C;

Pascal's semicolon rules, for no apparent reason, are a bit less regular and more complex to learn than the more widely used rules of the C family of languages. Apparently people understand C's semicolon-as-statement-terminator more easily than they understand pascal's semicolon-as-statement-seperator.

However, these rules are not a significant hurdle to reading or creating pascal code. In all the examples given here, the compiler will tell you if you get it wrong.

I disagree with Ariels on block layout. When coding a block, assuming that your compiler allows it, always end each statement with a semicolon, even the last one in a block. This just inserts a null statement (wich as far as I know doesn't affect your compiled program at all). The semicolon is optional but recommended. It is just more consistent between single and block statements, and involves less tinkering as you change things around.

Looking at code examples:

if n = 0 then
  Result := 1;    // use a semicolon

Introducing an else means removing the semicolon:

if n = 0 then
  Result := 1    // no semicolon
else
  Result := 2;   // use a semicolon

Or if you extend the code to a block:

if n = 0 then
begin
  ShowMessage('N is now zero');
  Result := 1;    // use a semicolon here
end;

I personally find a begin..end block balanced with a single stament to be unaesthetic, so I usually use begin..end on both sides of the else. It's more wordy in the source, but is more readable and as far as I know doesn't affect the compiled program at all. After you extend the code some more:

if n = 0 then
begin
  ShowMessage('N is now zero');
  Result := 1;    // use a semicolon here
end   // no semicolon here - that's a syntax error
else
begin
  ShowMessage('N is not zero');
end;

For completeness, I will include the only semicolon error that I have ever seen in a compilable Pascal (OK, Delphi) program. This is when not to use a semicolon. I think this e.g. is not a pascal-specific error and has direct parallels in C.

if n = 0 then;
  Result := 1; // this line is always hit as it is not under the if statement.

Delphi is a good reason to use Pascal - or if you must be picky, Object Pascal.

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:
printf("N is no");
if (n == 0)      // parens optional in pascal, required in C
  printf("w");  // semicolon required in C, error in pascal!!!
else
  printf("t");
printf(" zero."); // semicolon added
This is the one that confuses most beginning students, and is the example left out of StrawberryFrog's wu above.

if-then-else using blocks in pascal

if n = 0 then // parens optional -- left out
 begin
  ShowMessage('N is now zero');
  Result := 1;    // some compilers will take this
 end    // never use a semicolon before else!
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!
else
 {
  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.

Log in or register to write something here or to contact authors.