Question 1 Report
A program uses a CASE statement to convert a day number (1-7) to a day name and determine if it is a weekday or weekend.
(a) Write pseudocode using a CASE statement that inputs a day number, outputs the day name, and outputs whether it is a "Weekday" or "Weekend". [5]
(b) State what would happen if the user enters the value 8. [1]
(c) Explain the advantage of using a CASE statement over multiple IF statements for this problem. [2]
(a) The CASE statement maps each day number to a name, and a simple IF determines weekday vs. weekend. [5]
DECLARE DayNum : INTEGER
DECLARE DayName : STRING
DECLARE DayType : STRING
OUTPUT "Enter day number (1-7): "
INPUT DayNum
CASE OF DayNum
1: DayName ← "Monday"
2: DayName ← "Tuesday"
3: DayName ← "Wednesday"
4: DayName ← "Thursday"
5: DayName ← "Friday"
6: DayName ← "Saturday"
7: DayName ← "Sunday"
OTHERWISE: DayName ← "Invalid"
ENDCASE
IF DayNum >= 1 AND DayNum <= 5 THEN
DayType ← "Weekday"
ELSE
IF DayNum >= 6 AND DayNum <= 7 THEN
DayType ← "Weekend"
ELSE
DayType ← "Invalid"
ENDIF
ENDIF
OUTPUT DayName, " - ", DayType[1 mark for CASE structure with all 7 days, 1 mark for OTHERWISE handling, 1 mark for weekday/weekend classification, 1 mark for handling invalid input, 1 mark for output]
(b) If the user enters 8, the OTHERWISE clause executes and DayName becomes "Invalid". The weekday/weekend IF also falls through to the outer ELSE, producing DayType = "Invalid". The output would be "Invalid - Invalid". [1]
(c) A CASE statement is more readable when there are many discrete values to test. Each value appears on its own labelled branch, so the structure reads like a lookup table. [1] With IF statements, seven comparisons would need nesting, making the code harder to follow and increasing the chance of logic errors when adding or reordering options. [1]
Everything you need to excel in your exams