What is selection?
Selection is a programming construct that checks a condition and decides which instructions run. In contrast, sequence follows instructions in their written order without choosing between branches.
Think of a road junction: if the bridge is open, cross it; otherwise, take the diversion. The condition decides the route. This is the analogy in the FutureLogic Selection Knowledge Library lesson.
Conditions produce TRUE or FALSE
Conditions use comparison operators such as =, <, >, <=, >= and <> (not equal). The result is a Boolean value: TRUE or FALSE.
| Condition | Input | Result |
|---|---|---|
| Mark >= 50 | 50 | TRUE |
| Mark > 50 | 50 | FALSE |
| Age < 16 | 14 | TRUE |
IF ... THEN ... ENDIF: one-way selection
When the condition is TRUE, the IF body executes. When FALSE, it is skipped.
For Temperature = 32, the message appears. For Temperature = 25, this IF statement produces no output.
IF ... ELSE ... ENDIF: two-way selection
ELSE provides an alternative for a FALSE condition. Only one branch of this IF ... ELSE statement executes.
| Mark | Condition | Output |
|---|---|---|
| 72 | TRUE | Pass |
| 50 | TRUE | Pass |
| 49 | FALSE | Not yet |
Check your understanding 1
What is output if Mark is exactly 50?
Reveal answer
Pass. The comparison includes the boundary value.
ELSEIF: several conditions in order
ELSEIF tests another condition only when earlier alternatives are FALSE. The first TRUE branch runs and the other alternatives are skipped.
Score 85 gives High; 65 gives Medium; 30 gives Low. Testing the higher threshold first matters.
CASE: several fixed choices
CASE is helpful when a single value must be compared against several fixed alternatives. This is an additional worked example expanding on the Knowledge Library overview.
Choice 2 outputs Settings. Choice 7 follows OTHERWISE and outputs Invalid choice.
| IF / ELSEIF | CASE |
|---|---|
| Useful for ranges and different conditions | Useful for fixed alternatives of one expression |
| Example: Score >= 50 | Example: menu option 1, 2 or 3 |
Nested selection
Nested selection places one decision inside another. The inner condition is only checked when execution reaches it.
Age 14 with HasTicket TRUE outputs Enter. Age 10 outputs Ask an adult without checking the inner IF.
Check your understanding 2
What is output for Age 14 and HasTicket FALSE?
Reveal answer
Buy a ticket. The outer condition is TRUE, but the inner condition is FALSE.
How to trace a selection statement
- Record the input values.
- Evaluate each reached condition as TRUE or FALSE.
- Follow only the selected branch.
- Stop testing alternatives after a matching branch.
- Record the exact output and continue after ENDIF or ENDCASE.
Common mistakes
Only the chosen branch executes.
Check whether the boundary value is included.
The remaining alternatives are skipped.
Each IF needs its own matching ENDIF.
Exam focus: original FutureLogic questions
These are original Cambridge-style practice questions, not official exam questions. Mark allocations are illustrative.
Question 1 (2 marks)
Explain the purpose of selection in programming.
Reveal model answer
Question 2 (3 marks)
Write pseudocode to input a number and output Positive if greater than zero, otherwise Not positive.
Reveal model answer
INPUT Number
IF Number > 0 THEN
OUTPUT "Positive"
ELSE
OUTPUT "Not positive"
ENDIFQuestion 3 (3 marks)
Explain why CASE may be suitable for a menu with options 1, 2 and 3.
Reveal model answer
Question 4 (3 marks)
Trace the ELSEIF example for Score = 50, 79 and 80.
Reveal model answer
Quick quiz
- What two values can a condition produce?
- What happens when an IF condition is FALSE and there is no ELSE?
- When is ELSEIF evaluated?
- When is CASE useful?
- What is nested selection?
Quiz answers
Reveal all answers
- TRUE and FALSE.
- The IF body is skipped.
- When previous conditions in the chain were FALSE.
- When one expression has several fixed alternatives.
- A selection statement inside another selection statement.
Final summary
IF
Runs statements when a condition is TRUE.
ELSE
Provides the alternative branch.
ELSEIF
Tests another condition.
CASE
Selects from fixed values.