FutureLogic Blog → Programming → IGCSE 0478

How do IF, ELSE and CASE statements work?

IF, ELSE and CASE statements allow a computer program to make decisions. They use conditions or matching values to select which instructions should run, enabling a program to respond differently to different inputs.

In Cambridge IGCSE Computer Science (0478), selection is one of the three fundamental programming constructs, alongside sequence and iteration. This guide explains IF, ELSE, ELSEIF and CASE statements using clear pseudocode, worked examples and exam-style questions.

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.

Check a condition. Make a decision. Follow the appropriate route.

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.

ConditionInputResult
Mark >= 5050TRUE
Mark > 5050FALSE
Age < 1614TRUE
Exam tip: > 50 excludes 50; >= 50 includes it. Always check boundary values.

IF ... THEN ... ENDIF: one-way selection

When the condition is TRUE, the IF body executes. When FALSE, it is skipped.

IF Temperature > 30 THEN OUTPUT "Hot day" ENDIF

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.

INPUT Mark IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Not yet" ENDIF
MarkConditionOutput
72TRUEPass
50TRUEPass
49FALSENot 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.

INPUT Score IF Score >= 80 THEN OUTPUT "High" ELSEIF Score >= 50 THEN OUTPUT "Medium" ELSE OUTPUT "Low" ENDIF

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.

INPUT Choice CASE OF Choice 1 : OUTPUT "Start" 2 : OUTPUT "Settings" 3 : OUTPUT "Exit" OTHERWISE : OUTPUT "Invalid choice" ENDCASE

Choice 2 outputs Settings. Choice 7 follows OTHERWISE and outputs Invalid choice.

IF / ELSEIFCASE
Useful for ranges and different conditionsUseful for fixed alternatives of one expression
Example: Score >= 50Example: 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.

INPUT Age INPUT HasTicket IF Age >= 12 THEN IF HasTicket = TRUE THEN OUTPUT "Enter" ELSE OUTPUT "Buy a ticket" ENDIF ELSE OUTPUT "Ask an adult" ENDIF

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

  1. Record the input values.
  2. Evaluate each reached condition as TRUE or FALSE.
  3. Follow only the selected branch.
  4. Stop testing alternatives after a matching branch.
  5. Record the exact output and continue after ENDIF or ENDCASE.

Common mistakes

✗ Running both IF and ELSE.

Only the chosen branch executes.

✗ Confusing > with >=.

Check whether the boundary value is included.

✗ Testing ELSEIF after a previous TRUE branch.

The remaining alternatives are skipped.

✗ Forgetting ENDIF in nested statements.

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
Selection checks a condition and uses the result to decide which instructions execute.

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"
ENDIF

Question 3 (3 marks)

Explain why CASE may be suitable for a menu with options 1, 2 and 3.

Reveal model answer
CASE compares one menu-choice value against several fixed options. Each valid value selects an action. OTHERWISE can handle an invalid option.

Question 4 (3 marks)

Trace the ELSEIF example for Score = 50, 79 and 80.

Reveal model answer
50 → Medium; 79 → Medium; 80 → High.

Quick quiz

  1. What two values can a condition produce?
  2. What happens when an IF condition is FALSE and there is no ELSE?
  3. When is ELSEIF evaluated?
  4. When is CASE useful?
  5. What is nested selection?

Quiz answers

Reveal all answers
  1. TRUE and FALSE.
  2. The IF body is skipped.
  3. When previous conditions in the chain were FALSE.
  4. When one expression has several fixed alternatives.
  5. 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.

Selection does not guess. It follows the result of a condition.

Ready to practise programming?

Build confidence with FutureLogic's programming lessons and revision resources.