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

Lesson 9: else if & switch

Using C's else if and switch statements for multi-way conditional branching.

πŸ“– What to learn on this page
βœ… Must-know essentials
  • Chain else if for multiple branches
  • switch (x) { case 1: ... break; default: ...; }
  • Always break; at the end of each case
⭐ Read if you have time
  • Intentional fall-through
  • case labels must be constant expressions
  • switch doesn't work on strings

else if — three or more branches

An if statement gives a two-way "true or false" split. For three or more cases, use else if.
int score = 75;
if (score >= 90)      printf("Excellent\n");
else if (score >= 70) printf("Good\n");
else if (score >= 50) printf("Pass\n");
else                   printf("Fail\n");
Important: Branches are checked top to bottom, and only the first branch that evaluates to true runs. The rest are skipped.
Order matters: If score >= 50 comes first, even 90 and 70 match there and everyone "Passes". Put the strictest condition first.

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!
Intentional use: When you want "1 or 2 does the same thing", you can omit break on purpose.
case 1: case 2: printf("small\n"); break;

Try it yourself — switch

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

Related Lessons

Conditionals
Lesson 8: if Statement
How to write if statements in C. Visualize conditional branching with flowcharts.
Loops
Lesson 10: 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 12: Quiz (if)
Next lesson →
Lesson 14: 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.