Standard Algorithm Structures · Cambridge syllabus 7.9, 8.1 · Lesson 2 of 3
Selection
Selection chooses a path by evaluating a Boolean condition. IF with THEN and ELSE selects between branches; nested selection places another IF inside a branch, with its own ENDIF; CASE OF chooses one of several branches by the value of one variable, with OTHERWISE for anything else. Design conditions carefully at boundaries and ensure each required case is handled.
By the end, you can
- Use IF, THEN and ELSE structures.
- Use nested selection.
- Use multiple conditions.
- Trace conditional algorithms.
- Use CASE statements.
Conditional logic
Selection chooses between alternatives according to a condition. IF condition, then THEN on its own line, a block, and ENDIF runs the block only when the condition is true; adding ELSE gives a second block that runs when it is false, and exactly one of the two always runs. THEN and ELSE are indented two spaces from the IF.
The condition is an expression that is either true or false, built with the relational operators = for equal, <> for not equal, and <, >, <= and >=. It may combine several with AND, OR and NOT.
Nested selection puts one IF inside another, which is how a sequence of grade boundaries is written. There is no ELSE IF keyword in 0478 pseudocode: the second IF goes inside the ELSE, with an ENDIF of its own. IF Mark >= 70, THEN Grade ← "A", ELSE, and inside that ELSE, IF Mark >= 60, THEN Grade ← "B", ELSE Grade ← "C", ENDIF, and finally the outer ENDIF. The order matters enormously, because a mark of 85 satisfies every one of those conditions and must meet the highest first.
Where one variable is compared against many separate values, a CASE statement is clearer than nested IFs: CASE OF Choice, then one line per value such as 1 : OUTPUT "Start", with an OTHERWISE for anything not listed, closed by ENDCASE. The cases are tested in order, only the first that matches runs, and OTHERWISE must be the last.
Three faults recur. Using = when assignment was meant, or the arrow when comparison was meant. Testing conditions in the wrong order, so an earlier one catches a value the later one was for. And forgetting that the boundary value itself has to be covered — >= and > are different rules, and only one of them is the one you wanted.
Worth remembering
- IF, THEN, ELSE, ENDIF; with ELSE exactly one branch always runs.
- No ELSE IF: nest the IF inside the ELSE, with its own ENDIF, highest boundary first.
- CASE OF, then one value per line, OTHERWISE last, then ENDCASE.
Worked example
IF Score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Retry" ENDIF includes exactly 50 in the pass branch, because >= is true when Score is 50.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
Score >= 40.
Check your method, units and explanation against it.
Marked practice
Set 1 of 5 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.