Choosing between IF and CASE

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

Choosing between IF and CASE

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program reads a menu choice (1, 2, 3 or 4) and runs a different action for each. Which structure suits this best?
A IF Choice > 0 AND Choice < 5
B CASE Choice OF
2. A program prints 'Adult' if Age >= 18. Which structure suits this test best?
A IF Age >= 18 THEN
B CASE Age >= 18 OF
3. Which statement about CASE is correct?
A It compares one variable with several fixed values.
B It tests several different variables joined with AND and OR.
4. A program must check: Score >= 50 AND Attendance >= 90. Which structure should you use?
A CASE
B IF
5. A nested IF checks Choice = 1, then Choice = 2, then Choice = 3 inside ELSE. Which is easier to read?
A Rewriting it as CASE Choice OF
B Keeping the long nested IF

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: OUTPUT "Start" 2: OUTPUT "Quit" ENDCASE ACASE was used for a score test with a comparison operator (>=). CASE clauses match fixed values or ranges, so use IF Score >= 50 THEN ... ELSE ... ENDIF.
2IF Age >= 13 AND Age <= 19 THEN OUTPUT "Teenager" ENDIF BA menu choice: one variable (Choice) is matched against fixed values, so CASE is the neat choice.
3A CASE statement can be rewritten as nested IF by testing the same variable against each fixed value in turn. CDeeply nested IF is used for a menu where one variable is matched against fixed values. This is hard to read and should be a CASE OF Choice statement instead.
4Nested IF testing one variable for fixed values is easier to read when rewritten as a CASE statement. DAn age test using relational and logical operators, so IF is the right choice, not CASE.
5CASE OF Score >= 50: OUTPUT "Pass" OTHERWISE OUTPUT "Fail" ENDCASE EIF Choice = 1 THEN OUTPUT "Start" ELSE IF Choice = 2 THEN OUTPUT "Quit" ENDIF ENDIF
6IF Choice = 1 THEN OUTPUT "Start" ELSE IF Choice = 2 THEN OUTPUT "Quit" ELSE IF Choice = 3 THEN OUTPUT "Help" ENDIF ENDIF ENDIF FCASE OF Day 1: OUTPUT "Mon" 2: OUTPUT "Tue" OTHERWISE OUTPUT "Other" ENDCASE

Definitions

CASE OF

Fill in the Blanks

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

1.
_______ choice 1: OUTPUT "Play" ENDCASE
2.
IF age __ 18 THEN OUTPUT "Adult" ENDIF
3.
IF score >= 50 ___ score < 70 THEN OUTPUT "Merit" ENDIF
4.
IF choice = 1 THEN OUTPUT "Play" ____ OUTPUT "Quit" ENDIF
5.
CASE OF mark 1 __ 4: OUTPUT "Low" ENDCASE

Spot the Error

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

Question 1
A OUTPUT "1 Play 2 Quit"
B INPUT Choice
C CASE Choice
D 1 : OUTPUT "Playing"
E 2 : OUTPUT "Bye"
F ENDCASE
Question 2
A INPUT Score
B IF Score >= 70
C THEN OUTPUT "Distinction"
D OTHERWISE OUTPUT "Pass"
E ENDIF
Question 3
A INPUT Mark
B CASE OF Mark
C 0 TO 49 : OUTPUT "Fail"
D Mark >= 50 : OUTPUT "Pass"
E ENDCASE
Question 4
A INPUT Age
B IF Age < 13 AND Age > 19
C THEN OUTPUT "Not a teen"
D ENDIF
Question 5
A OUTPUT Option
B CASE OF Option
C "A" : OUTPUT "Add"
D "Q" : OUTPUT "Quit"
E ENDCASE

Answer Key

Choosing between IF and CASE
Section A: Quick Questions
Q1
B — CASE Choice OF
Q2
A — IF Age >= 18 THEN
Q3
A — It compares one variable with several fixed values.
Q4
B — IF
Q5
A — Rewriting it as CASE Choice OF
Section B: Match the Pairs
Q1
CASE OF Choice 1: OUTPUT "Start" 2: OUTPUT "Quit" ENDCASE → A menu choice: one variable (Choice) is matched against fixed values, so CASE is the neat choice.
Q2
IF Age >= 13 AND Age <= 19 THEN OUTPUT "Teenager" ENDIF → An age test using relational and logical operators, so IF is the right choice, not CASE.
Q3
A CASE statement can be rewritten as nested IF by testing the same variable against each fixed value in turn. → IF Choice = 1 THEN OUTPUT "Start" ELSE IF Choice = 2 THEN OUTPUT "Quit" ENDIF ENDIF
Q4
Nested IF testing one variable for fixed values is easier to read when rewritten as a CASE statement. → CASE OF Day 1: OUTPUT "Mon" 2: OUTPUT "Tue" OTHERWISE OUTPUT "Other" ENDCASE
Q5
CASE OF Score >= 50: OUTPUT "Pass" OTHERWISE OUTPUT "Fail" ENDCASE → CASE was used for a score test with a comparison operator (>=). CASE clauses match fixed values or ranges, so use IF Score >= 50 THEN ... ELSE ... ENDIF.
Q6
IF Choice = 1 THEN OUTPUT "Start" ELSE IF Choice = 2 THEN OUTPUT "Quit" ELSE IF Choice = 3 THEN OUTPUT "Help" ENDIF ENDIF ENDIF → Deeply nested IF is used for a menu where one variable is matched against fixed values. This is hard to read and should be a CASE OF Choice statement instead.
Section C: Definitions
Q1
CASE OF — Use CASE OF when one variable is matched against fixed values, like a menu choice. Use IF when you test conditions with <, > or AND/OR, like age or score tests.
Section D: Fill in the Blanks
Q1
CASE OF choice 1: OUTPUT "Play" ENDCASE
Q2
IF age >= 18 THEN OUTPUT "Adult" ENDIF
Q3
IF score >= 50 AND score < 70 THEN OUTPUT "Merit" ENDIF
Q4
IF choice = 1 THEN OUTPUT "Play" ELSE OUTPUT "Quit" ENDIF
Q5
CASE OF mark 1 TO 4: OUTPUT "Low" ENDCASE
Section E: Spot the Error
Q1
C — A menu choice is a good fit for CASE, but the keyword OF is missing. The line should be CASE OF Choice.
Q2
D — OTHERWISE belongs in a CASE statement. An IF statement uses ELSE for the alternative, so the line should be ELSE OUTPUT "Pass".
Q3
D — A CASE clause holds fixed values or ranges, not a relational test like >=. Write 50 TO 100 : OUTPUT "Pass", or use an IF statement for >= tests.
Q4
B — No age can be below 13 and above 19 at the same time, so AND makes the condition always false. Use OR: IF Age < 13 OR Age > 19.
Q5
A — The program must read the user's menu choice before the CASE statement tests it. The line should be INPUT Option, not OUTPUT.