C switch Statement

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is a switch Statement?

switch picks one path by matching an integer expression to a case label. Use break to leave the switch; use default when nothing matches. Prefer else if for ranges or non-integer tests.

Remember
switch (expr) {
    case 1:
        /* ... */
        break;
    case 2:
        /* ... */
        break;
    default:
        /* no match */
        break;
}

expr and case labels → integer types only
omit break → fall through to the next case
UsePrefer
One value vs many constantsswitch
Ranges / mixed conditionselse if
Simple true/falseif / if-else

How to Use switch in C

Match an integer, write a case for each constant, and end most cases with break.

  1. Put an integer expression in switch (expr).
  2. Add case constant: labels (compile-time integer constants).
  3. End each case with break unless you want fall-through.
  4. Add default: for unexpected values, then compile and run.
PieceRequired?Role
switch (expr)YesValue to match (integer type)
case labelsUsuallyConstant targets to jump to
breakUsuallyStops fall-through after a match
defaultRecommendedRuns when no case matches

Pseudocode

Pseudocode
evaluate expr

if expr equals case A:
    run A statements
    stop (break) or fall into next case
else if expr equals case B:
    run B statements
    ...
else:
    run default (if present)

continue after the switch

Cheat Sheet

Keep these facts handy while writing multi-way branches.

TopicDetail
Syntaxswitch (e) { case c: ... break; default: ... }
Allowed typesint, char, enum, other integers
Not allowedfloat, double, strings
case valuesCompile-time constants; no duplicates
breakLeaves the switch; omit only for intentional fall-through
defaultOptional catch-all; place anywhere (often last)
vs else ifswitch for constants; else if for ranges

Live Preview

Trace a small switch in the browser. Same control flow as the C examples below.

Browser simulation of switch matching (same idea as the C examples).

Result
day = 2

switch (day)
  case 1 → skip
  case 2 → match → "Tuesday" → break

output: Tuesday

Worked Walkthrough — Day 3

Follow switch (day) when day = 3 with cases for Monday–Sunday and a default.

Calculation
day = 3

jump to case 3:
  print "Wednesday"
  break → leave switch

cases 1, 2, 4… never run
default never runs (we matched)

If day were 0: no case matches → default
If case 3 had no break: fall into case 4 (bug!)

C Examples

Compile with gcc file.c -std=c11 -o out. Pair with break and else if when choosing a style.

Example 1 — Map a Day Number to a Name

One integer, several constant labels, and a break after each case.

C
#include <stdio.h>

int main(void) {
    int day = 2;

    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        default:
            printf("Unknown day\n");
            break;
    }

    return 0;
}

How It Works

  1. Evaluate the expression. day is 2.
  2. Jump to the matching label. Control goes to case 2:.
  3. Run that case. Prints Tuesday.
  4. Break out. Without break, execution would continue into case 3.

Example 2 — Catch Invalid Input with default

When no case matches, default runs. Good for menus and validation.

C
#include <stdio.h>

int main(void) {
    int choice = 9;

    switch (choice) {
        case 1:
            printf("New game\n");
            break;
        case 2:
            printf("Load game\n");
            break;
        case 3:
            printf("Quit\n");
            break;
        default:
            printf("Invalid choice: %d\n", choice);
            break;
    }

    return 0;
}

How It Works

  1. Choice is 9. None of the menu cases match.
  2. default runs. Prints the invalid-choice message.
  3. Still use break. Keeps the pattern consistent if you reorder cases later.
  4. Safer menus. Unexpected input gets a clear response instead of silence.

Example 3 — Intentional Fall-Through

Stack labels when several values should share the same code. Comment the fall-through so readers know it is deliberate.

C
#include <stdio.h>

int main(void) {
    int day = 6;  /* 6 = Saturday, 7 = Sunday */

    switch (day) {
        case 6:
        case 7:
            /* fall through: both weekend days */
            printf("Weekend\n");
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            printf("Weekday\n");
            break;
        default:
            printf("Invalid day\n");
            break;
    }

    return 0;
}

How It Works

  1. Match case 6. There is no body yet—execution falls into case 7.
  2. Shared body. Both Saturday and Sunday print Weekend.
  3. Then break. Stops before weekday cases run.
  4. Document it. A short comment prevents “missing break” confusion in review.

Edge Cases & Pitfalls

Check these before using switch in real programs.

break

Forgot break

Execution falls into the next case. Almost always a bug unless you documented intentional fall-through.

types

Wrong type for switch

No float, double, or string expressions. Use else if / strcmp for those.

range

Trying ranges in case

case 80...100 is not portable ISO C. Use else if for score bands.

default

No default

Unexpected values do nothing. Add default for menus and user input.

Performance Notes

ApproachCostBest for
switch (dense ints)Often a jump tableMany adjacent constants
else if chainLinear checksRanges, mixed conditions
Few casesEither is finePick the clearer form

Compilers often optimize dense switch cases well. Prefer clarity first; micro-optimizing branch style rarely matters for beginners.

Key Takeaways

  • Role: multi-way branch on one integer expression.
  • break: end each case unless fall-through is intentional.
  • default: catch unexpected values in menus and input.
  • Limits: integers only—use else if for ranges and strings.

One line: switch means “jump to the matching case—then usually break.”

Frequently Asked Questions

switch compares one integer expression against labeled case values. When a case matches, its statements run until break or the end of the switch. An optional default handles no match.
The switch expression and case labels must be integer types: int, char, short, long, or enum. C does not allow switch on float, double, or strings. Compare strings with if-else or strcmp.
break exits the switch block immediately. Without break, execution falls through into the next case. Always use break unless you deliberately want fall-through.
Fall-through happens when a case has no break and execution continues into the following case(s). It can be a bug or intentional—for example grouping case 6 and case 7 for weekend.
Use switch when comparing one variable to many constant values (menu 1/2/3, day 1–7). Use else if for ranges (score >= 80) or unrelated conditions.
No, but include default to catch unexpected input. It improves robustness and makes debugging easier when a value does not match any case.

Next: break Statement

Exit a switch or loop as soon as you are done.

break tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful