Ad space (banner)
๐Ÿ”งC Lessons
Lesson 14 / 68

The switch Statement

This lesson covers branching on several possible values with a switch statement in C, more readable than a long chain of if statements. It's for anyone searching "C switch statement usage."

A switch statement is a more readable way to branch on a single value than stacking up if / else if. Don't forget break.

The example prints a different message for case 1 through case 3 depending on the value of day, falling back to default for "some other day" otherwise. Needing a break at the end of every case is a distinctive feature of C.

A common early mistake is forgetting break. Skip it, and execution falls through into the next case's logic โ€” you need a break at the end of every case.

In real projects, this is a frequently-used construct for organizing a branch with many options more clearly than a chain of if/else.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)