Pascal & Object Pascal DISCUSSION

Pascal if-then-else: why is a semicolon before else an error, and where do semicolons belong?

Started by dattakakade Pascal semicolon rulesif then elsebegin end blocksempty statementdangling else
4 replies 248 views 5 participants
Latest activity · 30 Sep 2026

Pascal if-then-else: why is a semicolon before else an error, and where do semicolons belong?

dattakakade Pascal & Object Pascal Forum
#1

Coming from C, I put a semicolon after every statement. Free Pascal rejects if x > 0 then WriteLn('positive'); else WriteLn('not positive'); with an error at else, yet a semicolon before end seems to be accepted everywhere. I also lost an hour on if Ready then; StartMotor;, which compiled and started the motor regardless of Ready.

What is the actual rule for semicolons in Pascal, and how do I structure nested if statements so that each else belongs to the if I intend?

Community replies 4

Re: Pascal if-then-else: why is a semicolon before else an error, and where do semicolons belong?

#2

In C the semicolon terminates a statement; in Pascal it separates two statements. if ... then ... else ... is one single statement, so a semicolon after the then part ends the whole if, and the compiler then finds a statement starting with else, which does not exist. Write it without: if x > 0 then WriteLn('positive') else WriteLn('not positive');.

The semicolon before end is accepted because Pascal allows an empty statement: between the last semicolon and end there is simply a statement consisting of nothing.

Re: Pascal if-then-else: why is a semicolon before else an error, and where do semicolons belong?

#3

That empty statement is also what bit you. In if Ready then; StartMotor; the then branch is the empty statement, the if ends at the semicolon, and StartMotor is the next, unconditional statement.

The same mistake after do is worse: while Count < 10 do; Inc(Count); loops forever on an empty body, and for i := 1 to 10 do; WriteLn(i); runs an empty loop ten times and then prints once. All of these are legal Pascal, so the compiler accepts them; enable hints and read them.

Re: Pascal if-then-else: why is a semicolon before else an error, and where do semicolons belong?

#4

When a branch needs more than one statement, wrap it in begin ... end, and do not put a semicolon after that end if an else follows: if Ok then begin A; B end else begin C; D end;. Inside the blocks the semicolons separate A from B and C from D; one before each inner end is optional.

A layout habit that prevents most errors: put end else begin together on one line, so the absence of a semicolon there is visible at a glance.

Re: Pascal if-then-else: why is a semicolon before else an error, and where do semicolons belong?

#5

For nesting, an else always pairs with the nearest preceding if that does not have one yet, whatever the indentation suggests. In if A then if B then X else Y; the else belongs to if B. To attach it to the outer test, enclose the inner statement: if A then begin if B then X end else Y;.

One exception to the no-semicolon rule is the case statement, whose own else part may follow a semicolon. That creates a trap: in case N of 1: if A then X; else Y; end; the else belongs to the case, not to the if.

TEP COMMUNITY