CHAPTER 10 / 26
C Switch
Learn how to select one option from several cases with switch.
01 — Syntax
switch (expression) {\n case value:\n // code\n break;\n default:\n // code\n}02 — Example
#include <stdio.h>
int main() {
int choice = 2;
switch (choice) {
case 1: printf("One"); break;
case 2: printf("Two"); break;
default: printf("Other");
}
return 0;
}03 — Important Points
- switch is useful for menu-style choices.
- break prevents accidental fall-through.
04 — Common Mistakes
- Forgetting break.
- Using duplicate case labels.
05 — Real-World Use
Switch statements are common in menu systems and command dispatch logic.
06 — Practice
Create a small program that demonstrates the main idea of C Switch.