Learn C
#include "controlflow/switch.h"

The switch Statement in C

#concept

switch dispatches on an integer-like value (int, char, enum) to labeled case branches — cleaner than long == chains when one variable decides everything. Execution falls through from the matching case into every case below unless break stops it: occasionally useful for shared handling, usually a forgotten-break bug.

Cases must be compile-time constant expressions, and default catches unmatched values. Compilers warn on missing breaks (-Wimplicit-fallthrough) and unhandled enum values — the two classic switch mistakes as build messages.

#how it works
1

Dispatch on one value

switch (day) jumps straight to case 3: — one expression tested against many constant labels.

2

break stops fallthrough

Without break, execution slides into the next case's statements. End every case with break (or return) unless sharing is deliberate.

3

default handles the rest

default: runs when nothing matched — the else of the switch world, ideal for invalid-input handling.

4

Constants only

Case labels must be compile-time constants — literals, enums, const-int expressions. Ranges and variables are not allowed.

#example
#include <stdio.h>
 
int main(void) {
int day = 3;
switch (day) {
case 1: printf("Mon\n"); break;
case 3: printf("Wed\n"); break;
default: printf("other\n"); break;
}
return 0;
}
Output
Wed
1day is 3, so control jumps directly to case 3:, skipping case 1:.
2Wed prints, then break exits — without it, default's other would print too.
3Exactly one line appears, proving dispatch hit precisely one case.
#use cases
Menu and command dispatch on a single integer or char
State machines with enum states, one case per state
Stacking cases for shared handling (weekend days, vowels)
Letting -Wswitch warn when a new enum value lacks its case

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.