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

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 just an extension of if/else, so it belongs there. This page focuses on the switch statement β€” a different construct that dispatches on an integer value.

switch statement — branching by value

A handy construct when you want to dispatch based 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)
Evaluates the value used for branching.
case value:
Runs when the expression equals this value.
break;
Leaves the switch (very important).
default:
Runs when no case matches.
switch can dispatch on int or char values only. It cannot dispatch on doubles or strings.

Why break matters — fallthrough

Without break; execution "falls through" into the next case and keeps running — a common 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
Suppose 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 run time when "everything is printed".
πŸ”΄ What if you write case 1; instead of case 1:?
Colon : and semicolon ; sit next to each other on the keyboard, so this typo happens often.
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 form a 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 also a label. default; may compile (as an empty statement) but it becomes a silent bug: the "no case matched" branch never runs.
Intentional fallthrough: When you want "1 or 2 does the same thing", you can omit break on purpose. Mark it with a comment so reviewers know it's intentional:
case 1: case 2: printf("small\n"); break; // intentional

Try it yourself — switch

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

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 continues 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 recommended because it lets you handle the case when nothing matches.

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

int
char
float

switch supports integer types (int, char, etc.) only. float and double cannot be used.

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

Recommended Books to Deepen Your Understanding

Combine this interactive site with books for more practice.

πŸ“˜
Painfully Learning C
by MMGames
A classic C book for beginners. Builds solid fundamentals with clear explanations.
View on Amazon
πŸ“—
New Clear C Language (Beginner)
by Bohyoh Shibata
Plenty of diagrams and exercises. Widely used as a university textbook.
View on Amazon
πŸ“™
The C Programming Language, 2nd Ed.
by Brian Kernighan & Dennis Ritchie
Known as K&R. The original C book. Ideal after completing the basics.
View on Amazon

* Links above are affiliate links. Purchases help support this site.