Theory
The menu problem
The marks program is becoming an app, and apps have menus:
1. View marks
2. View percentage
3. View grade
4. Exit
An if-else ladder can handle it: if choice is 1... else if 2... but it reads like bureaucracy, and every rung re-tests choice.
For "jump straight to MY option", C carries a purpose-built tool: switch-case, along with its two famous sidekicks, break and default.
Theory
The lift, not the staircase
An if-else ladder is a staircase: you pass every floor below yours, answering a question at each. switch is the lift: press 3, arrive at floor 3 directly. But this lift has a quirk: its doors do not close behind you. Unless you press the stop button (break), you walk on into floor 4, then 5... And for buttons that match no floor, the lift has a rescue landing: default.
Practical
The marks menu
#include <stdio.h>
int main() {
int choice, marks = 78;
printf("1.Marks 2.Percentage 3.Grade : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Marks: %d\n", marks);
break;
case 2:
printf("Percentage: %.2f\n", marks * 100.0 / 100);
break;
case 3:
printf("Grade: %c\n", marks >= 70 ? 'A' : 'B');
break;
default:
printf("Invalid choice\n");
}
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Theory
The rules of the lift
switch (expression): the expression must be an integer type (int or char; chars work because they are ASCII numbers). Never float, and never a string.case CONSTANT:: labels must be constants:case 1:,case 'A':. A condition likecase marks >= 35:is illegal, switch matches VALUES, it does not test ranges.break;: exits the switch. Without it, execution falls through into the next case's code, matching stops but running does not.default:: runs when nothing matched, C's "none of the above". Position is flexible; the end is convention.
Quiz
In the menu code, suppose case 1 had NO break. The user enters 1. What prints?
- Marks AND Percentage: execution falls through into case 2 until its break
- Only Marks: cases end automatically
- A compile error: break is compulsory
- Invalid choice
Show the answer
Marks AND Percentage: execution falls through into case 2 until its break
Matching happens once, but without break the code KEEPS RUNNING into the next case's statements, stopping only at case 2's break. Fall-through is legal C (occasionally used deliberately), which is why the compiler stays silent and why predict-the-output-with-missing-break is the most repeated switch question in existence.
Think first
Why switch cannot grade
A student tries: switch (marks) { case marks >= 70: ... }. It will not compile. From the lift picture, reason out WHY, and name the right tool for grading.
Show the answer
A lift's buttons are fixed floor numbers, not questions: switch compares its value against CONSTANT labels, so case marks >= 70: (a condition, and not constant) is illegal. Ranges are staircase work: the if-else ladder from last lesson grades; switch menus. One line for the exam: switch matches exact values; if handles ranges and conditions.
Watch out
Where marks leak
Forgotten breaks: the eternal fall-through, trace every case in predict-the-output questions. switch on a float (switch (percentage)): illegal, integer types only. Conditions as case labels: illegal, constants only. And do not forget default in menu programs; examiners look for invalid-input handling, and it is one line of easy marks.
Theory
Choosing between the two selectors
Your decision toolkit is now complete: ladder for ranges, switch for menus, and both appear together in real programs (a switch menu whose case 3 contains a grading ladder, exactly like today's code). What neither can do is repeat: process 60 students means 60 manual runs. Repetition, the third structured-programming shape, arrives next: while, do-while and for.
Summary
Key takeaways
- switch jumps directly to the case matching an int/char value.
- case labels are constants only: no floats, no conditions, no ranges.
- break exits the switch; without it execution FALLS THROUGH to the next case.
- default catches unmatched values, the none-of-the-above branch.
- switch for exact-match menus; if-else ladder for ranges.
- Memory hook: the lift with doors that never close on their own.