Question 1
2 marksA program shows a menu with six options, numbered 1 to 6, and the user inputs the number of their choice.
Explain why a CASE statement is more suitable than a series of IF statements for processing the choice.
Model answer
The choice is a single variable that has to be matched against six fixed values, which is what CASE is designed for. Each option goes on its own line, so it is much shorter and easier to read than six nested IFs, and OTHERWISE deals with any invalid number.
Explanation
Both structures are selection, but CASE suits one variable with many separate values (menus, codes, single letters), while IF suits conditions such as ranges or combinations with AND and OR. Six nested IFs would also break Cambridge's guideline of nesting no more than three levels deep.
Mark scheme
-
1 mark
A CASE statement compares one variable with a list of specific values, which is exactly what a menu choice needs
max 2; 1 mark for each point
-
1 mark
It is shorter / clearer / easier to read and maintain than many nested IF statements
-
1 mark
OTHERWISE handles any invalid choice in one place
-
1 mark
It avoids deep nesting and repeating the variable in every condition