Making Decisions with Conditionals
Every Pascal program that does more than execute a fixed sequence of statements needs to make decisions, and the language provides two core constructs for this: the if-then-else statement and the case statement. Both evaluate a condition and select which block of code to execute, but they differ in how many branches they handle cleanly and how readable they are once the number of possibilities grows.
Cricket analogy: A captain choosing between a spin bowler and a pacer at the death overs is exactly like an if-then-else: check the batsman's weakness, then pick one of two lines of attack, never both at once.
The if-then-else Statement
The general form is if <boolean expression> then <statement> else <statement>; where the else clause is optional. Because Pascal treats the entire if-then-else as a single statement, you must be careful with semicolon placement — putting a semicolon directly before else is a compile-time error because it terminates the statement early and orphans the else clause.
Cricket analogy: Just as a DRS review either confirms or overturns the umpire's original call with no result in between, Pascal's if-then-else must resolve to exactly one branch, and a stray semicolon before else is like the third umpire cutting the review short before a decision is reached.
program CheckAge;
var
age: Integer;
begin
Write('Enter age: ');
ReadLn(age);
if age >= 18 then
WriteLn('You are an adult.')
else if age >= 13 then
WriteLn('You are a teenager.')
else
WriteLn('You are a child.');
end.Nested Conditionals and begin/end Blocks
When conditionals are nested — an if inside the then-branch of another if — Pascal resolves an ambiguous else by attaching it to the nearest preceding if that lacks one, the classic 'dangling else' problem. Wrapping the inner if in a begin...end block removes the ambiguity by making clear which if the else belongs to, and it is good practice to do this for any nested conditional even when the compiler would resolve it correctly on its own.
Cricket analogy: When two overlapping no-ball appeals happen on the same delivery, the umpire must be explicit about which infraction the decision applies to, just as begin/end blocks make explicit which if a dangling else belongs to.
Tip: Even when Pascal's dangling-else rule would resolve a nested if correctly, wrapping the inner if in begin...end makes the intent unambiguous to future readers and to compilers with stricter dialects.
The case Statement
The case statement is Pascal's answer to long chains of else-if comparisons against the same ordinal-typed expression. Its form is case <expression> of <value1>: <statement1>; <value2>: <statement2>; ... end;, where each label can be a single constant, a comma-separated list, or a subrange, and an optional otherwise (or else, depending on dialect) clause catches any value not explicitly listed.
Cricket analogy: Selecting a fielding position based on the batsman's dismissal history — cover, gully, short leg, or a general 'else' of mid-on — is naturally a case statement rather than a chain of if-else checks.
program GradeReport;
var
score: Integer;
begin
Write('Enter score (0-100): ');
ReadLn(score);
case score of
90..100: WriteLn('Grade: A');
80..89: WriteLn('Grade: B');
70..79: WriteLn('Grade: C');
60..69: WriteLn('Grade: D');
else
WriteLn('Grade: F');
end;
end.The case expression's type must be ordinal (integer, char, boolean, or enumerated) — you cannot use a String or Real value directly in a case statement's selector.
- if-then-else evaluates a boolean expression and executes exactly one of two branches
- The else clause is optional; omitting it simply skips execution when the condition is false
- Never place a semicolon immediately before else — it terminates the if-statement early
- Nested ifs should use begin...end blocks to avoid the dangling-else ambiguity
- case-of is preferred over long if-else chains when branching on a single ordinal value
- case labels can be single values, comma-separated lists, or subranges like 90..100
- An otherwise (or else) clause in a case statement catches any unmatched value
Practice what you learned
1. What happens if you place a semicolon immediately before an else clause in Pascal?
2. In Pascal's dangling-else problem, an else with no explicit block binds to...
3. Which of these is a valid case label in Pascal?
4. What does the otherwise (or else) clause in a case statement do?
5. Which type is NOT valid as a case statement's selector expression?
Was this page helpful?
You May Also Like
Loops in Pascal
Master Pascal's three loop constructs — for, while, and repeat-until — and know when to use each.
Procedures and Functions
Learn how Pascal separates reusable code into procedures (no return value) and functions (return a value), and how to declare and call each.
Recursion in Pascal
Learn how Pascal functions and procedures can call themselves, and how to design correct base cases and recursive cases.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics