CASE OF ... ENDCASE (7c)

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

CASE OF ... ENDCASE (7c)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly starts a CASE statement that tests the variable Direction?
A CASE OF Direction
B CASE Direction THEN
2. Which keyword correctly ends a CASE statement?
A ENDIF
B ENDCASE
3. Which case line correctly moves the position left when the value is 'W'?
A 'W' = Position ← Position - 10
B 'W' : Position ← Position - 10
4. How should the case lines be laid out inside CASE OF ... ENDCASE?
A Each case indented by two spaces
B Each case written at the left margin with no indent
5. Direction is 'W'. Two case lines both start with 'W'. How many of them run?
A Only the first matching case
B Every matching case

Match the Pairs

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

Left columnRight column
1CASE OF Direction AThe 'W' case is not indented by two spaces under CASE OF
2ENDCASE BCASE OF Direction 'W' : Position ← Position - 10 ENDCASE
3Each case is a single value, a colon and a statement, indented two spaces, and the structure ends with ENDCASE CENDCASE is missing, so the CASE structure is never closed
4Only the first matching case runs, so if Direction is 'W' only the 'W' statement is carried out DStarts a CASE structure that compares the variable Direction with a list of single values
5CASE OF Direction 'W' : Position ← Position - 10 EMarks the end of the CASE structure
6CASE OF Direction 'W' : Position ← Position - 10 ENDCASE FCASE OF Direction 'W' : Position ← Position - 10 'E' : Position ← Position + 10 ENDCASE

Definitions

CASE OF ... ENDCASE

Fill in the Blanks

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

1.
_______ Direction 'W' : Position ← Position - 10 ENDCASE
2.
CASE OF Direction 'W' : Position ← Position - 10 _______
3.
CASE OF Direction 'W' : Position ← Position _ 10 ENDCASE
4.
CASE OF Direction 'W' _ Position ← Position - 10 ENDCASE
5.
CASE OF Direction 'W' : Position _ Position - 10 ENDCASE

Spot the Error

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

Question 1
A CASE Key
B 'W' : Pos ← Pos - 10
C 'S' : Pos ← Pos + 10
D ENDCASE
Question 2
A CASE OF Key
B 'W' : Pos ← Pos - 10
C 'S' Pos ← Pos + 10
D ENDCASE
Question 3
A CASE OF Move
B 'L' : Pos ← Pos - 1
C 'R' ; Pos ← Pos + 1
D ENDCASE
Question 4
A CASE OF Key
B 'W' : Pos ← Pos - 10
C 'S' : Pos ← Pos + 10
D ENDIF
Question 5
A CASE OF Key
B 'W' : Pos ← Pos - 10
C 'S' : Pos ← Pos + 10
D ELSE OUTPUT "Invalid"
E ENDCASE

Answer Key

CASE OF ... ENDCASE (7c)
Section A: Quick Questions
Q1
A — CASE OF Direction
Q2
B — ENDCASE
Q3
B — 'W' : Position ← Position - 10
Q4
A — Each case indented by two spaces
Q5
A — Only the first matching case
Section B: Match the Pairs
Q1
CASE OF Direction → Starts a CASE structure that compares the variable Direction with a list of single values
Q2
ENDCASE → Marks the end of the CASE structure
Q3
Each case is a single value, a colon and a statement, indented two spaces, and the structure ends with ENDCASE → CASE OF Direction 'W' : Position ← Position - 10 ENDCASE
Q4
Only the first matching case runs, so if Direction is 'W' only the 'W' statement is carried out → CASE OF Direction 'W' : Position ← Position - 10 'E' : Position ← Position + 10 ENDCASE
Q5
CASE OF Direction 'W' : Position ← Position - 10 → ENDCASE is missing, so the CASE structure is never closed
Q6
CASE OF Direction 'W' : Position ← Position - 10 ENDCASE → The 'W' case is not indented by two spaces under CASE OF
Section C: Definitions
Q1
CASE OF ... ENDCASE — Compares one variable with a list of single values and runs the statement for the first match only. Each case is indented two spaces and the structure ends with ENDCASE.
Section D: Fill in the Blanks
Q1
CASE OF Direction 'W' : Position ← Position - 10 ENDCASE
Q2
CASE OF Direction 'W' : Position ← Position - 10 ENDCASE
Q3
CASE OF Direction 'W' : Position ← Position - 10 ENDCASE
Q4
CASE OF Direction 'W' : Position ← Position - 10 ENDCASE
Q5
CASE OF Direction 'W' : Position ← Position - 10 ENDCASE
Section E: Spot the Error
Q1
A — The OF keyword is missing. A CASE statement must start CASE OF followed by the identifier, so the line should be CASE OF Key.
Q2
C — The colon is missing after the case value. Each case needs a value, then a colon, then the statement: 'S' : Pos ← Pos + 10.
Q3
C — A semicolon is used instead of a colon. The case value and its statement must be separated by a colon: 'R' : Pos ← Pos + 1.
Q4
D — A CASE statement must finish with ENDCASE, not ENDIF. ENDIF is only for IF statements.
Q5
D — A CASE statement uses OTHERWISE for the default action, not ELSE. The line should be OTHERWISE OUTPUT "Invalid".