Nested selection with CASE and IF

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

Nested selection with CASE and IF

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which shows an IF correctly nested inside a CASE clause?
A CASE OF Grade 'A' : IF Score > 90 THEN OUTPUT "Top" ENDIF ENDCASE
B CASE OF Grade 'A' : IF Score > 90 THEN OUTPUT "Top" ENDCASE
2. An IF branch contains a CASE statement. Which closing order is correct?
A ENDCASE then ENDIF
B ENDIF then ENDCASE
3. A CASE clause contains an IF. Which closing order is correct?
A ENDCASE then ENDIF
B ENDIF then ENDCASE
4. True or false: a CASE clause can run an IF statement.
A False
B True
5. Which statement about nested selection is correct?
A Each IF needs its own ENDIF and each CASE needs its own ENDCASE
B One ENDIF can close both an inner IF and the outer CASE

Match the Pairs

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

Left columnRight column
1CASE OF Choice 1: IF Age >= 16 THEN OUTPUT "OK" ENDIF ENDCASE AThe closing keywords are in the wrong order. The inner CASE must end with ENDCASE before the outer IF ends with ENDIF.
2IF Score > 0 THEN CASE OF Grade 'A': OUTPUT "Top" ENDCASE ENDIF BIF Member = TRUE THEN CASE OF Level 1: OUTPUT "Bronze" 2: OUTPUT "Silver" ENDCASE ENDIF
3The inner structure must be closed first, so the closing keywords appear in the reverse order to the opening ones: ENDIF then ENDCASE. CAn IF statement placed inside a CASE clause. The IF is closed with ENDIF first, then the CASE is closed with ENDCASE.
4An IF branch can contain a CASE, and that CASE needs its own ENDCASE before the IF's ENDIF. DThe IF inside the CASE clause has no ENDIF. Add ENDIF after the OUTPUT line, before ENDCASE.
5CASE OF Choice 1: IF Age >= 16 THEN OUTPUT "OK" ENDCASE EA CASE statement placed inside an IF branch. The CASE is closed with ENDCASE first, then the IF is closed with ENDIF.
6IF Score > 0 THEN CASE OF Grade 'A': OUTPUT "Top" ENDIF ENDCASE FCASE OF Day 1: IF Wet = TRUE THEN OUTPUT "Bring coat" ENDIF ENDCASE

Definitions

CASE OF ... IF ... ENDIF ... ENDCASE

Fill in the Blanks

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

1.
CASE OF Option 1 : __ Score > 50 THEN OUTPUT "Pass" ENDIF ENDCASE
2.
CASE OF Option 2 : IF Lives = 0 THEN OUTPUT "Over" _____ ENDCASE
3.
CASE OF Option 1 : IF Score > 50 THEN OUTPUT "Pass" ENDIF _______
4.
IF Member = TRUE THEN ____ OF Level 1 : OUTPUT "Gold" ENDCASE ENDIF
5.
IF Age > 12 THEN CASE OF Level 1 : OUTPUT "Easy" ENDCASE _____

Spot the Error

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

Question 1
A CASE Choice
B 1 : IF Score > 50 THEN
C OUTPUT "Pass"
D ENDIF
E ENDCASE
Question 2
A IF Age >= 16
B CASE OF Role
C "S" : OUTPUT "Student"
D OTHERWISE OUTPUT "Other"
E ENDCASE
F ENDIF
Question 3
A CASE OF Level
B 1 IF Lives = 0 THEN
C OUTPUT "Game over"
D ENDIF
E ENDCASE
Question 4
A IF Open = TRUE THEN
B CASE OF Day
C "SAT" : OUTPUT Half day
D OTHERWISE OUTPUT "Full"
E ENDCASE
F ENDIF
Question 5
A CASE OF Grade
B "A" : IF Mark > 90 THEN
C OUTPUT "Top"
D ENDCASE
E ENDCASE

Answer Key

Nested selection with CASE and IF
Section A: Quick Questions
Q1
A — CASE OF Grade 'A' : IF Score > 90 THEN OUTPUT "Top" ENDIF ENDCASE
Q2
A — ENDCASE then ENDIF
Q3
B — ENDIF then ENDCASE
Q4
B — True
Q5
A — Each IF needs its own ENDIF and each CASE needs its own ENDCASE
Section B: Match the Pairs
Q1
CASE OF Choice 1: IF Age >= 16 THEN OUTPUT "OK" ENDIF ENDCASE → An IF statement placed inside a CASE clause. The IF is closed with ENDIF first, then the CASE is closed with ENDCASE.
Q2
IF Score > 0 THEN CASE OF Grade 'A': OUTPUT "Top" ENDCASE ENDIF → A CASE statement placed inside an IF branch. The CASE is closed with ENDCASE first, then the IF is closed with ENDIF.
Q3
The inner structure must be closed first, so the closing keywords appear in the reverse order to the opening ones: ENDIF then ENDCASE. → CASE OF Day 1: IF Wet = TRUE THEN OUTPUT "Bring coat" ENDIF ENDCASE
Q4
An IF branch can contain a CASE, and that CASE needs its own ENDCASE before the IF's ENDIF. → IF Member = TRUE THEN CASE OF Level 1: OUTPUT "Bronze" 2: OUTPUT "Silver" ENDCASE ENDIF
Q5
CASE OF Choice 1: IF Age >= 16 THEN OUTPUT "OK" ENDCASE → The IF inside the CASE clause has no ENDIF. Add ENDIF after the OUTPUT line, before ENDCASE.
Q6
IF Score > 0 THEN CASE OF Grade 'A': OUTPUT "Top" ENDIF ENDCASE → The closing keywords are in the wrong order. The inner CASE must end with ENDCASE before the outer IF ends with ENDIF.
Section C: Definitions
Q1
CASE OF ... IF ... ENDIF ... ENDCASE — A CASE clause can hold an IF, and an IF branch can hold a CASE. Close the inner structure first with its own ENDIF or ENDCASE, then close the outer one.
Section D: Fill in the Blanks
Q1
CASE OF Option 1 : IF Score > 50 THEN OUTPUT "Pass" ENDIF ENDCASE
Q2
CASE OF Option 2 : IF Lives = 0 THEN OUTPUT "Over" ENDIF ENDCASE
Q3
CASE OF Option 1 : IF Score > 50 THEN OUTPUT "Pass" ENDIF ENDCASE
Q4
IF Member = TRUE THEN CASE OF Level 1 : OUTPUT "Gold" ENDCASE ENDIF
Q5
IF Age > 12 THEN CASE OF Level 1 : OUTPUT "Easy" ENDCASE ENDIF
Section E: Spot the Error
Q1
A — A CASE statement must start with CASE OF followed by the variable. The word OF is missing. It should be CASE OF Choice.
Q2
A — The outer IF line needs the keyword THEN after its condition. It should be IF Age >= 16 THEN.
Q3
B — Each CASE clause needs a colon between its value and the statement it runs. The line should be 1 : IF Lives = 0 THEN.
Q4
C — Text to be output must be inside quote marks. Without them Half day looks like variable names. It should be OUTPUT "Half day".
Q5
D — The inner IF must be closed with its own ENDIF before the CASE is closed. Using ENDCASE here is wrong. It should be ENDIF.