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.