The semicolon, ; is used to separate statements (unlike other programming languages where it is used to terminate a statement). It is mandatory for most statements, voluntary in some statements, and forbidden in one particular statement.
(In order to explain usage, where Pascal keywords are referred to in the text of this article, they are shown in UPPER case. Program listings will use lower case.)
The semicolon is generally mandatory, and is required after every statement. The places where it is optional are
- before an END statement
A semicolon is forbidden
- immediately before an ELSE statement that belongs to an if statement
Notice that a semicolon can occur immediately before an else statement that belongs to a case statement.
Consider the following:
case a of true: if b then DoB; else DoNotA; end;
The else part belongs to the case statement.
DoNotA will be executed if none of the case labels (just one in this case) applies.
Now if you leave out the semicolon after DoB
case a of true: if b then DoB else DoNotB; end;
The else statement now is part of the "if b".
DoNotB will only be executed if a is true, and b is false.