When a decision depends on one variable taking one of many fixed values, such as a menu choice 1, 2, 3 or 4, a long else if ladder becomes hard to read. The switch statement expresses this kind of choice directly.
How switch works
Syntax
switch (expression) { case constant1: statements break; case constant2: statements break; ... default: statements}
The expression is evaluated once.
Control jumps to the case label whose constant equals that value.
Statements run from there until a break or the end of the switch.
If no case matches, control jumps to default (if present); otherwise the whole switch is skipped.
Rules for a switch statement:
Rule
Example
The expression must be an integer type (int, char, long, enum)
switch (choice), switch (grade)
Each case label must be a constant integer expression
case 1:, case 'A':, case MAX:
Case values must be unique
two case 2: labels are an error
float, double and strings cannot be used
case 2.5: is an error
default is optional and may appear anywhere
usually written last
break and fall-through
Without break, execution continues into the next case’s statements. This is called fall-through. It is useful when several values share one action (stack the labels), and a bug when a break is forgotten.
switch versus else-if
Aspect
switch
else-if ladder
Tests
Equality with constant integer values
Any condition (ranges, &&, ||, floats)
Readability for many fixed values
Very clear
Long and repetitive
Ranges such as 60–75
Not possible directly
Natural
Typical use
Menus, command codes, character options
Grades, slabs, ranges
Key takeaways
switch compares one integer expression with constant case labels.
Execution starts at the matching label and continues until break or the end.
Stacked labels share code; a forgotten break causes unwanted fall-through.
default handles all unmatched values and is optional.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write a switch that prints “Vowel” for the lower-case letters a, e, i, o and u, and “Consonant” for any other letter stored in ch.
Show worked answer
switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': printf("Vowel\n"); break; default: printf("Consonant\n"); }. The five vowel labels are stacked so they share one action through intentional fall-through, the break stops execution before default, and every other character reaches default.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/decision-and-loop-control-statements/the-switch-statement/ to read this page.