Practical 06: Switch Statement and Menu-Driven Program
Practical status: Suggested practice practical for SPL Lab. This is the sixth exercise in this notes collection, not a verified official NU experiment number. Follow your department's current lab manual if its sequence or requirements differ.
1. Aim
To understand the switch statement in C and develop a simple menu-driven arithmetic program.
2. Learning Outcomes
After completing this practical, you should be able to:
- Explain the purpose and syntax of
switch,case,break, anddefault. - Select an operation based on a user's menu choice.
- Use
switchto handle multiple fixed choices. - Prevent division-by-zero, modulus-by-zero, and signed integer edge-case errors.
3. Theory
The switch statement is a decision-making statement. It executes one block of code from several possible case options, depending on the value of an expression.
For example, a calculator may show a menu:
- Addition
- Subtraction
- Multiplication
- Division
- Modulus
The user selects a number, and the program performs the corresponding operation.
Important keywords
| Keyword | Purpose |
|---|---|
switch |
Checks the value of an expression. |
case |
Defines a possible matching value. |
break |
Exits the switch block after a matching case. |
default |
Runs when no case matches. |
Syntax
switch (expression) {
case value1:
// statements
break;
case value2:
// statements
break;
default:
// statements
}
Why is break important? Without break, execution can continue into the statements of the next case. This is called fall-through.
4. Program: Menu-Driven Arithmetic Calculator
The program below takes two integers and lets the user choose an arithmetic operation.
C Code
#include <stdio.h>
#include <limits.h>
int main(void) {
int a, b, choice;
long long result;
printf("===== MENU =====\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Modulus\n");
printf("Enter your choice (1-5): ");
if (scanf("%d", &choice) != 1) {
printf("Invalid input.\n");
return 1;
}
printf("Enter two integers: ");
if (scanf("%d %d", &a, &b) != 2) {
printf("Invalid input.\n");
return 1;
}
switch (choice) {
case 1:
result = (long long)a + b;
printf("Addition = %lld\n", result);
break;
case 2:
result = (long long)a - b;
printf("Subtraction = %lld\n", result);
break;
case 3:
result = (long long)a * b;
printf("Multiplication = %lld\n", result);
break;
case 4:
if (b != 0) {
printf("Division = %.2f\n", (double)a / b);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
case 5:
if (b == 0) {
printf("Error: Modulus by zero is not allowed.\n");
} else if (a == INT_MIN && b == -1) {
printf("Error: Remainder is not representable as an int.\n");
} else {
printf("Modulus = %d\n", a % b);
}
break;
default:
printf("Invalid choice. Please select 1 to 5.\n");
}
return 0;
}
5. Explanation
#include <stdio.h>includes the standard input/output library.int a, b, choice;declares two integer operands and the menu choice.- The
printf()statements display the menu. scanf()reads the user's choice. Its return value is checked to ensure that one integer was read.- The second
scanf()reads the two operands. The program checks that both values were read. switch (choice)checks the selected menu number.- Each
caseperforms one operation. breakprevents execution from continuing into another case.- In division and modulus,
b != 0ensures the divisor is not zero. defaulthandles a choice outside the range 1–5.
Note: Division uses (double)a / b so that the result can contain a fractional part. Modulus (%) works with integers.
6. Algorithm
- Start.
- Display the arithmetic menu.
- Read the user's choice.
- Read two integer numbers.
- Check the choice using a
switchstatement. - Perform the selected operation.
- Before division or modulus, check whether the second number is zero; also handle the
INT_MINand-1edge case. - If the choice is invalid, display an error message.
- Stop.
7. Sample Input and Output
Example 1: Addition
===== MENU =====
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice (1-5): 1
Enter two integers: 15 5
Addition = 20
Example 2: Division
Enter your choice (1-5): 4
Enter two integers: 7 2
Division = 3.50
Example 3: Division by zero
Enter your choice (1-5): 4
Enter two integers: 10 0
Error: Division by zero is not allowed.
Example 4: Invalid choice
Enter your choice (1-5): 8
Enter two integers: 10 2
Invalid choice. Please select 1 to 5.
8. Dry Run
Suppose the user enters:
choice = 3a = 6b = 4
| Step | What happens | Result |
|---|---|---|
| 1 | Read the menu choice | choice = 3 |
| 2 | Read the numbers | a = 6, b = 4 |
| 3 | Evaluate switch(choice) |
case 3 matches |
| 4 | Calculate a * b |
6 * 4 = 24 |
| 5 | Execute break |
Exit the switch |
| 6 | Return from main() |
Program ends |
Output: Multiplication = 24
9. How to Compile and Run
Save the file as menu_calculator.c.
Using GCC in a terminal:
Windows:
gcc menu_calculator.c -o menu_calculator.exe
.\menu_calculator.exe
Linux / macOS:
gcc menu_calculator.c -o menu_calculator
./menu_calculator
If GCC is not installed or configured, use the C compiler/IDE available in your lab.
10. Common Mistakes
- Forgetting
breakafter a case. - Writing duplicate case values.
- Using a non-constant expression as a case label.
- Forgetting to handle invalid menu choices with
default. - Dividing by zero, calculating modulus by zero, or overlooking the
INT_MINand-1edge case. - Using
%with floating-point values. - Forgetting the address operator
&inscanf().
11. Practice Tasks
Try these independently:
- Create a menu-driven program to convert temperatures from Celsius to Fahrenheit and Fahrenheit to Celsius.
- Use
switchto display the name of a day based on a number from 1 to 7. - Create a menu-driven area calculator for a circle, rectangle, and triangle.
- Modify the calculator to ask whether the user wants to perform another operation.
12. Viva Questions
Q1. What is a switch statement? A: A selection statement that executes a matching case according to the value of an expression.
Q2. What is the purpose of the case keyword? A: It defines a possible value that can match the switch expression.
Q3. Why is break used in a switch statement? A: It exits the switch block and prevents unintended fall-through.
Q4. What is the purpose of default? A: It executes when none of the case values match.
Q5. Is default compulsory? A: No. It is optional, but useful for handling unexpected choices.
Q6. Can we use floating-point values as case labels? A: No. Case labels must be constant integral values (or compatible enumeration constants).
Q7. What happens if break is omitted? A: Execution may continue into the following case statements.
Q8. What is the difference between division (/) and modulus (%)? A: Division produces a quotient, while modulus gives the remainder for integer operands.
13. Result
The menu-driven arithmetic calculator was designed using the switch statement. It performs the selected arithmetic operation and handles invalid choices and zero divisors.
Learning note: This is a suggested practice exercise organized around SPL topics. Check your current department lab manual for the required practical sequence and report format.