Choosing between CASE and IF

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

Choosing between CASE and IF

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program reads a menu choice of 1, 2 or 3 and runs a different action for each. Which structure fits best?
A CASE Choice OF
B Three separate IF statements that each test ranges
2. A program converts a mark into a grade: A for 80 or more, B for 60 to 79, C for 40 to 59. Which structure fits best?
A CASE Mark OF
B IF ... THEN ... ELSE
3. Which condition CANNOT be handled neatly by a CASE statement?
A 1: OUTPUT "Start game"
B IF Age >= 13 AND Score > 50 THEN
4. Which line is a correct case in a CASE statement for a menu?
A 2: OUTPUT "Load game"
B 2 TO 4: OUTPUT "Load game"
5. A user types a letter: 'S' to save, 'L' to load, 'Q' to quit. Which should you use?
A CASE Letter OF
B IF Letter > 'A' AND Letter < 'Z' THEN

Match the Pairs

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

Left columnRight column
1CASE OF Choice AIF Age >= 16 AND HasTicket = TRUE THEN
2IF Mark >= 70 THEN BTests one variable, Choice, against several single values such as 1, 2 or 3, like a menu
3A menu where the user picks 1, 2 or 3 is one variable tested against single values, so use CASE CCASE OF Choice 1 : OUTPUT "Play" 2 : OUTPUT "Quit" ENDCASE
4A condition that combines two variables cannot be written as a CASE, so use IF DTests a range of values, so IF is used rather than CASE, as in grade boundaries
5CASE OF Mark 70 TO 100 : Grade ← "A" ENDCASE EGrade boundaries are ranges, so CASE is the wrong choice. Use IF Mark >= 70 THEN instead.
6IF Choice = 1 THEN OUTPUT "Play" ELSE IF Choice = 2 THEN OUTPUT "Quit" ENDIF ENDIF FA menu tests one variable against single values, so CASE OF Choice is clearer than long nested IF statements.

Definitions

CASE OF

Fill in the Blanks

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

1.
____ Choice OF 1 : OUTPUT "Play" ENDCASE
2.
CASE Choice __ 2 : OUTPUT "Quit" ENDCASE
3.
IF Mark __ 70 THEN OUTPUT "Merit" ENDIF
4.
IF Age >= 13 ___ Score > 50 THEN OUTPUT "Pass" ENDIF
5.
CASE Choice OF 3 : OUTPUT "Help" _______

Spot the Error

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

Question 1
A INPUT Choice
B CASE OF Choice
C 1 TO 2 : OUTPUT "Play"
D 2 : OUTPUT "Quit"
E ENDCASE
Question 2
A IF Mark >= 70 THEN
B Grade ← "A"
C OTHERWISE
D Grade ← "B"
E ENDIF
Question 3
A INPUT Choice
B CASE OF Choice
C 1 : OUTPUT "Play"
D 2 OUTPUT "Quit"
E ENDCASE
Question 4
A INPUT Pct
B IF Pct >= 40 AND < 60 THEN
C OUTPUT "Merit"
D ENDIF
Question 5
A INPUT Option
B CASE Option OF
C 1 : OUTPUT "Open"
D 2 : OUTPUT "Save"
E ENDCASE

Answer Key

Choosing between CASE and IF
Section A: Quick Questions
Q1
A — CASE Choice OF
Q2
B — IF ... THEN ... ELSE
Q3
B — IF Age >= 13 AND Score > 50 THEN
Q4
A — 2: OUTPUT "Load game"
Q5
A — CASE Letter OF
Section B: Match the Pairs
Q1
CASE OF Choice → Tests one variable, Choice, against several single values such as 1, 2 or 3, like a menu
Q2
IF Mark >= 70 THEN → Tests a range of values, so IF is used rather than CASE, as in grade boundaries
Q3
A menu where the user picks 1, 2 or 3 is one variable tested against single values, so use CASE → CASE OF Choice 1 : OUTPUT "Play" 2 : OUTPUT "Quit" ENDCASE
Q4
A condition that combines two variables cannot be written as a CASE, so use IF → IF Age >= 16 AND HasTicket = TRUE THEN
Q5
CASE OF Mark 70 TO 100 : Grade ← "A" ENDCASE → Grade boundaries are ranges, so CASE is the wrong choice. Use IF Mark >= 70 THEN instead.
Q6
IF Choice = 1 THEN OUTPUT "Play" ELSE IF Choice = 2 THEN OUTPUT "Quit" ENDIF ENDIF → A menu tests one variable against single values, so CASE OF Choice is clearer than long nested IF statements.
Section C: Definitions
Q1
CASE OF — Use CASE OF to test one variable against single values, like a menu choice. Use IF for ranges, like grade boundaries, or when conditions combine several variables.
Section D: Fill in the Blanks
Q1
CASE Choice OF 1 : OUTPUT "Play" ENDCASE
Q2
CASE Choice OF 2 : OUTPUT "Quit" ENDCASE
Q3
IF Mark >= 70 THEN OUTPUT "Merit" ENDIF
Q4
IF Age >= 13 AND Score > 50 THEN OUTPUT "Pass" ENDIF
Q5
CASE Choice OF 3 : OUTPUT "Help" ENDCASE
Section E: Spot the Error
Q1
C — Each CASE branch tests one single value, like a menu choice. A range such as 1 TO 2 needs an IF statement. It should be 1 : OUTPUT "Play".
Q2
C — OTHERWISE belongs only in a CASE statement. An IF statement uses ELSE for the other branch.
Q3
D — Each CASE value must be followed by a colon before its statement. It should be 2 : OUTPUT "Quit".
Q4
B — A range needs IF with a full condition on each side of AND. The variable must be repeated: Pct >= 40 AND Pct < 60.
Q5
B — The CASE header is written CASE OF followed by the variable being tested. It should be CASE OF Option.