5.1 Understanding IF Statements
An IF statement is a control structure that allows a program to make a decision. It checks a condition — an expression that is either TRUE or FALSE — and runs different code depending on the result. This is how programs decide what to do next.
Key idea: A condition is always evaluated as either TRUE or FALSE — never maybe, never both. The IF statement simply asks "is this TRUE?" and branches accordingly.
The Basic Structure
IF <condition> THEN
<statements to run if TRUE>
ENDIFA Real Example
DECLARE Age : INTEGER
INPUT Age
IF Age >= 18 THEN
OUTPUT "You can vote."
ENDIFIn this example, the program asks the user for their age. If the value entered is 18 or more, the condition Age >= 18 is TRUE and the message "You can vote." is displayed. If the age is less than 18, the condition is FALSE and the program simply skips the OUTPUT line — nothing is printed.
Comparison Operators Used in Conditions
| Operator | Meaning | Example (TRUE when...) |
|---|---|---|
| = | Equal to | X = 5 — X is exactly 5 |
| != | Not equal to | X != 5 — X is anything other than 5 |
| > | Greater than | X > 5 — X is bigger than 5 |
| < | Less than | X < 5 — X is smaller than 5 |
| >= | Greater than or equal to | X >= 5 — X is 5 or bigger |
| <= | Less than or equal to | X <= 5 — X is 5 or smaller |
Remember: In CIE pseudocode the equality operator is a single = sign (not == like in Python or Java). And the assignment operator is <-.
Understanding IF Statements
5.2 The THEN Clause
The THEN clause contains the statements that run when the condition is TRUE. In CIE pseudocode, the THEN keyword must follow the condition on the same line, and the statements belonging to it are indented underneath.
Syntax
IF <condition> THEN
<statement 1>
<statement 2>
...
ENDIFYou can put as many statements as you like inside the THEN clause. All of them run only if the condition is TRUE. If the condition is FALSE, the whole block is skipped and the program jumps to the line after ENDIF.
Example: Multiple Statements in THEN
DECLARE Score : INTEGER
INPUT Score
IF Score >= 50 THEN
OUTPUT "Congratulations!"
OUTPUT "You passed the exam."
OUTPUT "Your score was " + Score
ENDIFIf the user enters 72, all three OUTPUT statements execute. If they enter 30, none of them run — the program flow jumps straight from the IF line to after ENDIF.
Indentation tip: Always indent the statements inside THEN by 2 spaces. This does not change how the program runs, but it makes it crystal clear which lines belong to the THEN branch — and examiners love well-laid-out code.
The THEN Clause
5.3 The ELSE Clause
The ELSE clause is OPTIONAL. It provides an alternative set of statements that run when the IF condition is FALSE. Together, IF/THEN/ELSE lets you handle both possible outcomes of a decision.
Syntax
IF <condition> THEN
<statements for TRUE>
ELSE
<statements for FALSE>
ENDIFExample: Pass or Fail
DECLARE Mark : INTEGER
INPUT Mark
IF Mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIFNow the program always outputs something. If Mark is 60, the condition is TRUE so "Pass" is printed. If Mark is 40, the condition is FALSE so the ELSE branch runs and "Fail" is printed instead.
IF...THEN...ELSE vs IF...THEN
| Feature | IF...THEN only | IF...THEN...ELSE |
|---|---|---|
| Has an ELSE branch? | No | Yes |
| When condition is TRUE | Runs THEN block | Runs THEN block |
| When condition is FALSE | Skips (nothing runs) | Runs ELSE block |
| Best used when | Action is optional | Two distinct outcomes |
Rule of thumb: Use ELSE whenever you want the program to do something in both the TRUE and FALSE cases. Without ELSE, the program silently does nothing when the condition fails.
The ELSE Clause
5.4 Nested IF Statements
Sometimes one decision is not enough. A nested IF is an IF statement placed inside the THEN or ELSE branch of another IF. It lets you test a second condition only after the first one has been resolved — perfect for grading systems, age brackets, or any time you have more than two possible outcomes.
Structure of a Nested IF
IF <condition 1> THEN
<statements if condition 1 is TRUE>
ELSE
IF <condition 2> THEN
<statements if condition 1 FALSE and condition 2 TRUE>
ELSE
<statements if both conditions are FALSE>
ENDIF
ENDIFExample: Grade Classifier
DECLARE Mark : INTEGER
INPUT Mark
IF Mark >= 75 THEN
OUTPUT "Grade A"
ELSE
IF Mark >= 60 THEN
OUTPUT "Grade B"
ELSE
IF Mark >= 50 THEN
OUTPUT "Grade C"
ELSE
OUTPUT "Grade F"
ENDIF
ENDIF
ENDIFNotice how each ENDIF closes the innermost IF first. Trace the logic with Mark = 65:
- 65 >= 75? FALSE → enter the ELSE branch
- 65 >= 60? TRUE → run inner THEN, OUTPUT "Grade B"
- The remaining nested IFs are skipped
ENDIF count rule: Every IF needs its own ENDIF. If you have 3 nested IFs, you need 3 ENDIFs at the bottom — and they must be in the correct order (innermost first, outermost last).
Worked Trace: X = 5
DECLARE X : INTEGER
X <- 5
IF X > 10 THEN
OUTPUT "A"
ELSE
IF X > 3 THEN
OUTPUT "C"
ELSE
OUTPUT "D"
ENDIF
ENDIFStep-by-step: X is 5. Is 5 > 10? No — so we go into the outer ELSE. Now is 5 > 3? Yes — so we run the inner THEN and OUTPUT "C". The inner ELSE ("D") is never reached.
Nested IF Statements
5.5 Key Points Summary
Let's recap the most important ideas about IF...THEN...ELSE before you tackle the Question Bank.
Exam tip: When tracing nested IFs in an exam, write the value of each condition (TRUE/FALSE) next to it as you go. This makes it almost impossible to lose track of which branch executes.