πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English

Lesson 10: switch

The switch statement β€” branching by integer value. case, break, default and fallthrough.

πŸ“– What to learn on this page
βœ… Must-know essentials
  • switch (x) { case 1: ... break; default: ...; } form
  • Always break; at the end of each case
  • default runs when no case matches
⭐ Read if you have time
  • Intentional fall-through (omitting break)
  • case labels must be constant expressions
  • switch works only on integer types (no strings)
πŸ’‘ else if is now covered in Lesson 8 (if statement). Chaining else if is really just an extension of if/else, so it belongs there. This page focuses on switch β€” a different construct that dispatches on an integer value.

switch statement — branching by value

A handy construct for dispatching on the value of a variable.
int day = 3;
switch (day) {
  case 1: printf("Monday\n"); break;
  case 2: printf("Tuesday\n"); break;
  case 3: printf("Wednesday\n"); break;
  default: printf("Unknown\n");
}
switch (expr)
The value to branch on.
case value:
Runs when the expression matches this value.
break;
Exits the switch (don't forget this).
default:
Runs when no case matches.
switch works on integer types only (int, char, and so on). You can't switch on doubles or strings.

Why break matters — fallthrough

Without break;, execution "falls through" into the next case and keeps running — a classic source of bugs.
switch (n) {
  case 1: printf("A\n");   // no break!
  case 2: printf("B\n");   // no break!
  case 3: printf("C\n"); break;
}
// When n=1: prints A, B, and C!
πŸ”΄ What actually happens when you forget break
Say you want to print the day of the week for day 1–3, but you forgot every single break:
int day = 1;
switch(day){
  case 1: printf("Mon\n");   // no break
  case 2: printf("Tue\n");   // no break
  case 3: printf("Wed\n");   // no break
  default: printf("Unknown\n");
}
Output (day=1):
Mon
Tue
Wed
Unknown

→ After matching case 1, execution keeps falling through cases 2, 3, and default.
The scary part: no compile error. You only notice at runtime, when "everything prints."
πŸ”΄ What if you write case 1; instead of case 1:?
The colon : and semicolon ; sit next to each other on the keyboard, so this typo comes up all the time.
switch(day){
  case 1; // ← typed ; instead of :
    printf("Mon\n");
    break;
}
Result: compile error (gcc):
error: expected ':' or '...' before ';' token
→ The compiler expects a : after case 1 to complete the label.
πŸ”‘ Memory hook: the : after case marks a label (same syntax as goto labels). Statements end with ;; labels end with :.
⚠️ Related trap: default; instead of default:
default is a label too. default; may compile (as an empty statement), but it becomes a silent bug β€” the "no case matched" branch never runs.
Intentional fallthrough: when "1 or 2 should do the same thing," you can drop break on purpose. Add a comment so reviewers know it's deliberate:
case 1: case 2: printf("small\n"); break; // intentional

Try it yourself — switch

switch.c
Output
Press "Run" to execute...
πŸ’‘ Try these ideas too

Related Lessons

Conditionals
Lesson 9: if Statement
How to write if statements in C. Visualize conditional branching with flowcharts.
Loops
Lesson 11: for & while Loops
How to use for and while loops in C.
Reference
C Cheat Sheet
Quick reference for printf, operators, types, and more.
← Previous lesson
Lesson 14: Quiz (if)
Next lesson →
Lesson 16: Loops (for/while)

Review Quiz

Check your understanding of this lesson!

Q1. What happens if you omit break in a switch statement?

Compile error
Execution falls through into the next case
Nothing happens

Without break, execution keeps running into every case that follows the matched one. This is called "fallthrough."

Q2. Is default required in a switch statement?

Required
Optional (can be omitted)
Required when there are three or more cases

default is optional, but it's usually worth including so you have somewhere to handle unmatched values.

Q3. Which type cannot be used in a switch condition?

int
char
float

switch only accepts integer types (int, char, and so on). float and double aren't allowed.

Share this article
Share on X (Twitter) Share on Facebook Send on LINE Hatena