Teachers' Portal  /  IGCSE Computer Science 0478  /  Programming  /  Sequence and selection: IF and CASE
Lesson pack · IGCSE Computer Science 0478 · 8.1 · IGCSE

Sequence and selection: IF and CASE

Choose a path. Programs make decisions: pass or fail, valid or invalid, which menu option was chosen.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Programming
Syllabus reference
Cambridge IGCSE Computer Science 0478, 8.1
Level
IGCSE
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 2 of 11 in Programming; follows Variables, constants and data types; leads into Iteration: count-controlled, pre-condition and post-condition loops
Central model
Fig. 1: an IF statement follows one of two paths, then both paths meet again at ENDIF
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • IF... THEN... ELSE: In the lab, choose the Nested IF program and step through it.
  • Nested IF: An IF statement can sit inside the THEN or ELSE branch of another IF.
  • CASE OF: When one variable is compared with several single values, a CASE statement is shorter and easier to read than a chain of IFs.
Key points to leave on the board
  • IF: IF condition, THEN, optional ELSE, ENDIF.
  • Boundaries: decide whether the boundary value passes: > or >=.
  • Nested IF: test the highest band first; close every IF with its own ENDIF.
  • CASE: one variable, single values, first match only, OTHERWISE last.
The core visual model

One picture every idea returns to

Sequence means statements run in order, one after another. Selection means the program chooses which statements to run by testing a condition that is either TRUE or FALSE. An IF statement runs the THEN branch when the condition is TRUE and the optional ELSE branch when it is FALSE. A nested IF is an IF inside another IF. A CASE statement compares one variable with several single values and runs only the first match; OTHERWISE catches every other value.

INPUT Mark Mark >= 40 OUTPUT "Pass" OUTPUT "Fail" ENDIF TRUEFALSE THEN ELSE
Fig. 1: an IF statement follows one of two paths, then both paths meet again at ENDIF
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Choose a path.. Programs make decisions: pass or fail, valid or invalid, which menu option was chosen. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: IF... THEN... ELSEIn the lab, choose the Nested IF program and step through it. Project tab 1 of the lab (Selection with a trace table) and let a student drive it while the class predicts.Whole class, then pairs
13 to 21 minTeach and check: Nested IFAn IF statement can sit inside the THEN or ELSE branch of another IF. Model the worked example Dry run on the board, one line at a time. Your turn: What is output when Mark is 40?Pairs
21 to 30 minTeach and check: CASE OFWhen one variable is compared with several single values, a CASE statement is shorter and easier to read than a chain of IFs. Model the worked example The lab's CASE and REPEAT program on the board, one line at a time.Mix-Pair-Share
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 18 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in CASE OF, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Mix-Pair-Share

Students mix around the room until you call pair; they pair with the nearest classmate, discuss the prompt, then mix again. Three prompts of rising difficulty, three different partners.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
Check the boundary.With IF Age > 18 an 18-year-old would be told they are too young. Use >= when the boundary value itself should pass.
Test the highest band first.If the program tested Mark >= 40 first, a mark of 85 would output Pass and never reach Distinction.
OTHERWISE must be the last case, and it runs only if none of the cases above it match.Use an IF, not CASE, when a branch depends on a range such as Mark >= 40.
Worksheet

Sequence and selection: IF and CASE: worksheet

6 questions, 18 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · explain[2 marks]

Describe what is meant by selection and name two types of selection statement.

Q2 · write[4 marks]

Write pseudocode that inputs a temperature and outputs "Freezing" if it is below 0, "Cold" if it is from 0 to 15, and "Warm" if it is above 15.

Q3 · trace[3 marks]

State the output of this pseudocode when X is (a) 12 (b) 5 (c) 20.

IF X > 10
  THEN
    IF X > 15
      THEN
        OUTPUT "Large"
      ELSE
        OUTPUT "Medium"
    ENDIF
  ELSE
    OUTPUT "Small"
ENDIF
Q4 · write[4 marks]

Write a CASE statement that inputs a grade (a single character) and outputs "Excellent" for 'A', "Good" for 'B', "Fair" for 'C' and "Not recognised" for anything else.

Q5 · explain[2 marks]

Explain why a programmer includes an OTHERWISE clause in a CASE statement.

Q6 · analysis[3 marks]

This code should output "Merit" for marks of 60 or more and "Pass" for marks of 40 to 59. Identify the error and correct it.

IF Mark >= 40
  THEN
    OUTPUT "Pass"
  ELSE
    IF Mark >= 60
      THEN
        OUTPUT "Merit"
    ENDIF
ENDIF
Answer key and marking notes
Q1 · 2 marks
  • Selection: the program tests a condition and chooses which statements to run depending on whether it is TRUE or FALSE.
  • IF statements and CASE statements.
Q2 · 4 marks
  • INPUT Temp
  • IF Temp < 0 THEN OUTPUT "Freezing"
  • ELSE IF Temp <= 15 THEN OUTPUT "Cold" ELSE OUTPUT "Warm" (nested IF)
  • Correct ENDIF for each IF and THEN/ELSE structure
Q3 · 3 marks
  • (a) 12 > 10 TRUE, 12 > 15 FALSE: Medium
  • (b) 5 > 10 FALSE: Small
  • (c) 20 > 10 TRUE, 20 > 15 TRUE: Large
Q4 · 4 marks
  • INPUT Grade and CASE OF Grade
  • 'A' : OUTPUT "Excellent", 'B' : OUTPUT "Good"
  • 'C' : OUTPUT "Fair"
  • OTHERWISE OUTPUT "Not recognised" then ENDCASE
Q5 · 2 marks
  • It runs when the value matches none of the listed cases.
  • So unexpected or invalid values are still handled, e.g. with an error message.
Q6 · 3 marks
  • The conditions are in the wrong order: any mark of 60 or more is caught by Mark >= 40 and outputs Pass.
  • Merit can never be output (the inner IF only runs for marks below 40).
  • Correction: test Mark >= 60 first (output Merit), then in the ELSE test Mark >= 40 (output Pass).
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

In a CASE OF ... ENDCASE statement, when are the statements after OTHERWISE run?

  • AOnly when the value matches none of the cases listed above it
  • BEvery time, after the matching case has been run
  • COnly when the value matches more than one of the cases
  • DBefore any of the cases are checked
Q21 mark

A program should output "You can vote" for anyone aged 18 or over. Which condition should go in the IF statement?

  • AAge >= 18
  • BAge > 18
  • CAge <= 18
  • DAge = 18
Q31 mark

The program is run twice, first with the input 30 and then with the input 15. What is output?

DECLARE Temp : INTEGER
INPUT Temp
IF Temp > 30
  THEN
    OUTPUT "Hot"
  ELSE
    IF Temp >= 15
      THEN
        OUTPUT "Warm"
      ELSE
        OUTPUT "Cold"
    ENDIF
ENDIF

  • AWarm, then Warm
  • BHot, then Warm
  • CHot, then Cold
  • DWarm, then Cold
Q41 mark

A student writes this to grade a mark out of 100. What is output when Mark is 85?

IF Mark >= 40
  THEN
    OUTPUT "Pass"
  ELSE
    IF Mark >= 70
      THEN
        OUTPUT "Distinction"
      ELSE
        OUTPUT "Fail"
    ENDIF
ENDIF

  • APass
  • BDistinction
  • CDistinction, then Pass
  • DFail
Q51 mark

The inputs are R, R, L, X, Q in that order. What is output?

DECLARE Position : INTEGER
DECLARE Move : CHAR
Position ← 20
REPEAT
    INPUT Move
    CASE OF Move
      'L' : Position ← Position - 3
      'R' : Position ← Position + 4
      OTHERWISE OUTPUT "Beep"
    ENDCASE
UNTIL Move = 'Q'
OUTPUT Position

  • ABeep, Beep, 25
  • BBeep, 25
  • C25
  • DBeep, Beep, 22
Q61 mark

This program is meant to output the largest of three different whole numbers A, B and C.

IF A > B
  THEN
    OUTPUT A
  ELSE
    IF B > C
      THEN
        OUTPUT B
      ELSE
        OUTPUT C
    ENDIF
ENDIF
Which set of test data shows that the program is faulty?

  • AA = 3, B = 1, C = 9
  • BA = 1, B = 5, C = 2
  • CA = 2, B = 4, C = 7
  • DA = 8, B = 3, C = 6
Quiz key
Q1: A

CASE compares the variable with each listed value and runs only the first match; OTHERWISE is the last clause and runs only when no case matched. It is not run after a match, and a value cannot usefully match more than one single value. It is checked last, never first.

Q2: A

Age >= 18 is TRUE for 18 and every age above it. Age > 18 wrongly rejects an 18-year-old. Age <= 18 accepts children and rejects adults over 18. Age = 18 accepts only people who are exactly 18.

Q3: A

30 > 30 is FALSE, so the ELSE branch runs; 30 >= 15 is TRUE, so the first run outputs Warm. For 15, 15 > 30 is FALSE and 15 >= 15 is TRUE, so it also outputs Warm. Hot would need a temperature above 30, and Cold would need one below 15.

Q4: A

85 >= 40 is TRUE, so Pass is output and the ELSE branch (containing the Distinction test) is never reached. This is why the highest band must be tested first. Distinction cannot be output by this program for any mark, because a mark of 70 or more always passes the first test. Only one branch runs, so two outputs are impossible, and Fail needs a mark below 40.

Q5: A

R, R and L give 20 + 4 + 4 - 3 = 25. X matches no case, so Beep is output. Q is also checked by the CASE before the UNTIL test, and it matches no case either, so a second Beep is output; then the loop ends and 25 is output. Beep, 25 forgets that Q goes through the CASE. 25 ignores OTHERWISE. 22 counts the L as -6 or misses one R.

Q6: A

With A = 3, B = 1, C = 9, A > B is TRUE so 3 is output, but 9 is the largest: the program never compares A with C. For 1, 5, 2 it outputs 5; for 2, 4, 7 it outputs 7; for 8, 3, 6 it outputs 8. All three are correct, so they do not reveal the fault.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonVariables, constants and data types