OTHERWISE in a CASE statement (8.1b)

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

OTHERWISE in a CASE statement (8.1b)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where does OTHERWISE go in a CASE statement?
A As the last clause, after all the listed values
B As the first clause, before the listed values
2. When does the OTHERWISE clause run?
A Every time the CASE statement runs
B When the variable matches none of the listed values
3. Choice is 7 and the CASE only lists 1, 2 and 3, with no OTHERWISE. What happens?
A Nothing happens, no clause runs
B The program stops with an error message
4. Which line is the correct way to write an OTHERWISE clause?
A OTHERWISE OUTPUT "Invalid choice" ELSE
B OTHERWISE : OUTPUT "Invalid choice"
5. Choice is 2 and the CASE has 2 : OUTPUT "Option 2" and OTHERWISE : OUTPUT "Invalid choice". What is output?
A Option 2
B Option 2 and Invalid choice

Match the Pairs

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

Left columnRight column
1OTHERWISE : OUTPUT "Invalid choice" ARuns and displays an error message when the CASE variable matches none of the listed values
2CASE OF Choice 1 : OUTPUT "Start" 2 : OUTPUT "Quit" ENDCASE BOTHERWISE is placed before the listed value. It must be the last clause, so move it below 1 : OUTPUT "Start".
3OTHERWISE must be the last clause, placed after all the listed values and just before ENDCASE CThe colon is missing after OTHERWISE. It should be OTHERWISE : OUTPUT "Invalid choice".
4Use OTHERWISE to catch invalid menu choices and tell the user DCASE OF Grade 'A' : OUTPUT "Top" 'B' : OUTPUT "Good" OTHERWISE : OUTPUT "Try again" ENDCASE
5CASE OF Choice OTHERWISE : OUTPUT "Invalid choice" 1 : OUTPUT "Start" ENDCASE EA CASE with no OTHERWISE, so nothing happens if Choice is 3
6OTHERWISE OUTPUT "Invalid choice" FOTHERWISE : OUTPUT "Invalid choice"

Definitions

OTHERWISE

Fill in the Blanks

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

1.
CASE OF Choice 1 : OUTPUT "Play" _________ : OUTPUT "Invalid choice"
2.
1 : OUTPUT "Play" OTHERWISE : ______ "Invalid choice" ENDCASE
3.
2 : OUTPUT "Quit" OTHERWISE : OUTPUT "Invalid choice" _______
4.
CASE __ Menu OTHERWISE : OUTPUT "Invalid choice" ENDCASE
5.
3 : OUTPUT "Exit" _________ : OUTPUT "Invalid choice" ENDCASE

Spot the Error

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

Question 1
A CASE OF Choice
B 1 OUTPUT "Start"
C OTHERWISE : OUTPUT "Error"
D ENDCASE
Question 2
A CASE Menu
B 1 : OUTPUT "Play"
C 2 : OUTPUT "Quit"
D OTHERWISE : OUTPUT "Error"
E ENDCASE
Question 3
A CASE OF Day
B 1 : OUTPUT "Mon"
C 2 : OUTPUT "Tue"
D OTHERWISE OUTPUT "Error"
E ENDCASE
Question 4
A CASE Grade OF
B "A" : OUTPUT "Top"
C "B" : OUTPUT "Good"
D OTHERWISE : OUTPUT "Error"
E ENDCASE
Question 5
A CASE OF Choice
B 1 : OUTPUT "Start"
C ELSE : OUTPUT "Error"
D ENDCASE

Answer Key

OTHERWISE in a CASE statement (8.1b)
Section A: Quick Questions
Q1
A — As the last clause, after all the listed values
Q2
B — When the variable matches none of the listed values
Q3
A — Nothing happens, no clause runs
Q4
B — OTHERWISE : OUTPUT "Invalid choice"
Q5
A — Option 2
Section B: Match the Pairs
Q1
OTHERWISE : OUTPUT "Invalid choice" → Runs and displays an error message when the CASE variable matches none of the listed values
Q2
CASE OF Choice 1 : OUTPUT "Start" 2 : OUTPUT "Quit" ENDCASE → A CASE with no OTHERWISE, so nothing happens if Choice is 3
Q3
OTHERWISE must be the last clause, placed after all the listed values and just before ENDCASE → CASE OF Grade 'A' : OUTPUT "Top" 'B' : OUTPUT "Good" OTHERWISE : OUTPUT "Try again" ENDCASE
Q4
Use OTHERWISE to catch invalid menu choices and tell the user → OTHERWISE : OUTPUT "Invalid choice"
Q5
CASE OF Choice OTHERWISE : OUTPUT "Invalid choice" 1 : OUTPUT "Start" ENDCASE → OTHERWISE is placed before the listed value. It must be the last clause, so move it below 1 : OUTPUT "Start".
Q6
OTHERWISE OUTPUT "Invalid choice" → The colon is missing after OTHERWISE. It should be OTHERWISE : OUTPUT "Invalid choice".
Section C: Definitions
Q1
OTHERWISE — The last clause of a CASE statement. It runs when the variable matches none of the listed values, e.g. OTHERWISE : OUTPUT "Invalid choice". Without it, nothing happens if no value matches.
Section D: Fill in the Blanks
Q1
CASE OF Choice 1 : OUTPUT "Play" OTHERWISE : OUTPUT "Invalid choice"
Q2
1 : OUTPUT "Play" OTHERWISE : OUTPUT "Invalid choice" ENDCASE
Q3
2 : OUTPUT "Quit" OTHERWISE : OUTPUT "Invalid choice" ENDCASE
Q4
CASE OF Menu OTHERWISE : OUTPUT "Invalid choice" ENDCASE
Q5
3 : OUTPUT "Exit" OTHERWISE : OUTPUT "Invalid choice" ENDCASE
Section E: Spot the Error
Q1
B — Each CASE clause needs a colon between the value and the statement. It should be 1 : OUTPUT "Start".
Q2
A — A CASE statement starts with CASE OF followed by the variable. OF is missing; it should be CASE OF Menu. Without it, the OTHERWISE clause has no valid CASE to belong to.
Q3
D — OTHERWISE must be followed by a colon, just like the value clauses. It should be OTHERWISE : OUTPUT "Error".
Q4
A — The keywords come first, then the variable: CASE OF Grade. The words are in the wrong order. Then OTHERWISE can catch any grade that is not A or B.
Q5
C — ELSE belongs to IF statements. The last CASE clause that runs when no value matches is OTHERWISE. It should be OTHERWISE : OUTPUT "Error".