Decisions with if and else
Comparing values with the right operators, branching with if then else, and why a branch with two statements needs begin and end around it.
A condition is a boolean
The condition after if is any expression of type boolean: a comparison, a variable, or comparisons joined with and, or and not. Comparisons use =, <>, <, >, <= and >=.
Two of those are worth stopping on. = compares and := assigns; confusing them is the first mistake everyone makes, and the compiler catches it because the types do not match. <> means not equal, since Pascal has no bang or double equals.
Text compares too, letter by letter, so 'Ada' < 'Bob' is true. That makes sorting names a plain comparison rather than a call to a library.
The type matters because the compiler will not accept a number where a boolean belongs. if count then is a type error even though other languages treat zero as false; write if count > 0 then and say what you mean. The same strictness catches a comparison written with a single = in the wrong place: the compiler sees a boolean where a statement belongs and stops.
if, then, else
The shape is if condition then statement else statement. Both branches take exactly one statement, and only one of them runs.
There is no semicolon before else. A semicolon ends a statement, and the if is not finished until its else has been read, so putting one there ends the if early and leaves the else with nothing to belong to.
Chains work by nesting: if ... then ... else if ... then ... else .... Pascal reads them exactly as written, and the indentation in the examples is a reading aid rather than something the compiler needs.
An if is a statement, not an expression: it does something rather than producing a value, so it cannot sit on the right of an assignment. When a value has to be chosen between two options, write the assignment inside each branch, or move the choice into a function that returns the answer.
Two statements need a block
When a branch should do more than one thing, wrap the statements in begin … end. The block counts as one statement, so it fits where a single statement goes, and the semicolons inside it separate its own statements.
This is the only place a beginner needs begin and end in the middle of a program: for a group of statements where one is expected. The same block used after then needs no semicolon after its final end when an else follows.
A chain and a block
program Main;
var
score: integer;
begin
score := 72;
if score >= 90 then
writeln('A')
else if score >= 70 then
begin
writeln('B');
writeln('Solid pass');
end
else
writeln('C');
end.Which if owns the else
An else belongs to the nearest if above it that does not already have one. Indentation is invisible to the compiler, so a nested if can steal the else you meant for the outer branch even when the layout says otherwise. A begin … end around the inner branch settles the question.
and and or have an order too: not binds tightest, then and, then or. Comparisons bind tighter than all three, so score > 0 and score < 100 groups as two comparisons joined by and. When a condition mixes and with or, put the grouping you mean in brackets; the compiler will not infer it from the line breaks.
Boundaries are where branches go wrong
A chain of comparisons is only correct if the cases meet without gaps or overlaps. With score >= 90, score >= 70 and a final else, a score of 70 takes the second branch and a score of 69 takes the third; nothing falls between them. Reverse the order and the first test swallows everything above 70, so the later branches can never run.
Pick the boundaries first and write them down: 90 and above is an A, 70 to 89 is a B, below 70 is a C. Then write the tests from the most specific to the most general, and check the exact boundary values — 69, 70, 89, 90 — rather than a comfortable middle like 75. Bugs live at the edges, and a middle value exercises none of them.
Three decision mistakes to watch for: writing == or != from another language (Pascal uses = and <>), letting a nested if capture an else meant for the outer one, and comparing two reals with =. Wrap the inner if in begin … end, and compare reals with a tolerance rather than exact equality.
Try these next
- Rewrite the grade chain so a score of exactly 70 prints the B branch, and check the boundary.
- Add an inner
ifwithoutbeginandend, predict which branch takes theelse, then fix it with a block. - Join two comparisons with
orand add brackets so the meaning is obvious to a reader.
Grade a score
Run it, then change the score to 95 and to 40 and run again. Each change should take a different branch of the chain.
A semicolon between end and else is a compile error, and the message talks about an unexpected else. Delete the semicolon rather than the else.
Leap year, with brackets
Run it, then change the year to 1900 and to 2000. Predict which branch each one takes before you run it again.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
Which operator compares two values for equality in Pascal?
