CASE clauses with ranges and several statements

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

This CASE clauses with ranges and several statements worksheet is part of the IGCSE Computer Science 0478 — 8.1b Sequence, Selection and Iteration journey. Print it as a booklet?

CASE clauses with ranges and several statements

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which CASE clause correctly matches every whole number from 1 to 9?
A 1 TO 9 : OUTPUT "Low"
B 1 - 9 : OUTPUT "Low"
2. Which clause is a valid grade band for marks from 50 up to 69?
A 50 < Mark < 69 : Grade <- "B"
B 50 TO 69 : Grade <- "B"
3. Which pair of CASE clauses is written correctly for marks 0 to 100?
A 0 TO 49 : and 50 TO 100 :
B 0 TO 50 : and 50 TO 100 :
4. A clause has two indented statements under it. What happens when its range matches?
A Only the first statement runs
B Both indented statements run, in order
5. The clauses are 0 TO 49, 50 TO 74 and 75 TO 100. Mark is 75. Which clause runs?
A 75 TO 100
B 50 TO 74

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
11 TO 9 : A0 TO 49 : 50 TO 100 :
270 TO 100 : grade <- "A" OUTPUT grade BThe ranges overlap because 50 is in both 0 TO 50 and 50 TO 100. The first range should end at 49.
3A range clause for the grade band 50 to 69 that sets the grade to B CA CASE clause that matches any value from 1 up to 9, including both 1 and 9
4Ranges in the same CASE must not overlap, so each mark belongs to only one band D50 TO 69 : grade <- "B"
50 TO 50 : grade <- "F" 50 TO 100 : grade <- "P" EA range must be written with the keyword TO, not a hyphen. It should be 1 TO 9 :
6CASE OF mark 1 - 9 : OUTPUT "Low" ENDCASE FA range clause whose two indented statements both run when the value is between 70 and 100

Definitions

1 TO 9 :

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
CASE OF Score 0 __ 49 : OUTPUT "Fail" ENDCASE
2.
CASE OF Mark 50 TO 69 : ______ "Merit" ENDCASE
3.
CASE OF Mark 1 TO 9 : Total <- Total + Mark OUTPUT _____
4.
0 TO 49 : Grade <- "F" __ TO 100 : Grade <- "P" ENDCASE
5.
CASE OF Mark 1 TO 9 : OUTPUT "Low" _______

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A CASE OF Mark
B 70 TO 100 : OUTPUT "A"
C 40 - 69 : OUTPUT "B"
D ENDCASE
Question 2
A CASE OF Score
B 1 TO 9 OUTPUT "Low"
C 10 TO 99 : OUTPUT "High"
D ENDCASE
Question 3
A CASE OF Mark
B 70 TO 100 :
C OUTPUT "Pass"
D Passes <- Pass + 1
E ENDCASE
Question 4
A CASE Mark
B 50 TO 100 : OUTPUT "Pass"
C OTHERWISE : OUTPUT "Fail"
D ENDCASE
Question 5
A CASE OF Age
B 0 TO 13 : OUTPUT "Child"
C 13 TO 19 : OUTPUT "Teen"
D ENDCASE

Answer Key

CASE clauses with ranges and several statements
Section A: Quick Questions
Q1
A — 1 TO 9 : OUTPUT "Low"
Q2
B — 50 TO 69 : Grade <- "B"
Q3
A — 0 TO 49 : and 50 TO 100 :
Q4
B — Both indented statements run, in order
Q5
A — 75 TO 100
Section B: Match the Pairs
Q1
1 TO 9 : → A CASE clause that matches any value from 1 up to 9, including both 1 and 9
Q2
70 TO 100 : grade <- "A" OUTPUT grade → A range clause whose two indented statements both run when the value is between 70 and 100
Q3
A range clause for the grade band 50 to 69 that sets the grade to B → 50 TO 69 : grade <- "B"
Q4
Ranges in the same CASE must not overlap, so each mark belongs to only one band → 0 TO 49 : 50 TO 100 :
Q5
0 TO 50 : grade <- "F" 50 TO 100 : grade <- "P" → The ranges overlap because 50 is in both 0 TO 50 and 50 TO 100. The first range should end at 49.
Q6
CASE OF mark 1 - 9 : OUTPUT "Low" ENDCASE → A range must be written with the keyword TO, not a hyphen. It should be 1 TO 9 :
Section C: Definitions
Q1
1 TO 9 : — In a CASE clause, value1 TO value2 matches a whole range, such as 1 TO 9. Indented statements under the clause all run. Ranges in one CASE must not overlap.
Section D: Fill in the Blanks
Q1
CASE OF Score 0 TO 49 : OUTPUT "Fail" ENDCASE
Q2
CASE OF Mark 50 TO 69 : OUTPUT "Merit" ENDCASE
Q3
CASE OF Mark 1 TO 9 : Total <- Total + Mark OUTPUT Total
Q4
0 TO 49 : Grade <- "F" 50 TO 100 : Grade <- "P" ENDCASE
Q5
CASE OF Mark 1 TO 9 : OUTPUT "Low" ENDCASE
Section E: Spot the Error
Q1
C — A range in a CASE clause is written with TO, not a hyphen. It should be 40 TO 69 : OUTPUT "B".
Q2
B — The colon is missing after the range. A clause must be written 1 TO 9 : OUTPUT "Low".
Q3
D — The statement must add 1 to the same variable it assigns to. Pass is a different, undeclared variable. It should be Passes <- Passes + 1.
Q4
A — The CASE statement must start with CASE OF followed by the variable. The word OF is missing, so it should be CASE OF Mark.
Q5
B — Ranges in one CASE must not overlap. 0 TO 13 includes 13, which is also in 13 TO 19. It should be 0 TO 12.