Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 2 of 8

switch - Multi-way Branching & Fall-through

The `switch` statement is a multi-way branch on an **integer** value (or enum). Its defining characteristic is **fall-through** - execution continues into the next `case` unless explicitly broken with `break`. This is not a bug - it's a feature - but forgetting `break` is one of the most common C bugs. Understanding when fall-through is intentional vs accidental is crucial.

11 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

switch - Fall-through, default & Intentional Fall-through

switch - Fall-through, default & Intentional Fall-through
#include <stdio.h>

typedef enum { MON=1, TUE, WED, THU, FRI, SAT, SUN } Day;

void describe_day(Day d) {
    switch (d) {
        case MON:
            printf("Start of work week
");
            break;    /* break: exit the switch */
        case TUE:
        case WED:
        case THU:
            /* INTENTIONAL fall-through: all three cases do same thing */
            printf("Mid week
");
            break;
        case FRI:
            printf("TGIF!
");
            break;
        case SAT:
        case SUN:
            printf("Weekend!
");
            break;
        default:
            /* Runs if d is not any of the above values */
            printf("Unknown day: %d
", d);
            break;
    }
}

/* ── switch on char - very common pattern ────────────────── */
void parse_option(char opt) {
    switch (opt) {
        case 'v': printf("verbose mode
"); break;
        case 'h': printf("help
");         break;
        case 'o': printf("output file
");  break;
        default:
            printf("Unknown option: -%c
", opt);
            break;
    }
}

int main(void) {
    describe_day(WED);   /* Mid week */
    describe_day(SAT);   /* Weekend! */
    parse_option('v');   /* verbose mode */

    /* ── DANGEROUS: missing break ──────────────────────── */
    int x = 1;
    switch (x) {
        case 1:
            printf("case 1
");
            /* FORGOT break - falls into case 2! */
        case 2:
            printf("case 2 (fell through!)
");
            break;
        case 3:
            printf("case 3
");
            break;
    }
    /* Output: case 1
case 2 (fell through!) */

    return 0;
}

Common Mistakes

  • Missing `break` causing unintended fall-through - this is one of the most common C bugs. Always end each case with `break` unless intentional fall-through is explicitly documented with a comment.
  • Using `switch` on a float or string - switch works only on integer types (int, char, enum, short, long). For floating-point or string comparisons, use if/else chains.
  • Not having a `default` case - if the switch value doesn't match any case and there's no default, execution silently skips the whole switch. Always include default to catch unexpected values.

Tip

Tip

Practice switch Multiway Branching Fallthrough in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Switch compiles into efficient jump tables for constant integer and enum evaluations

Practice Task

Note

Practice Task - (1) Write a working example of switch Multiway Branching Fallthrough from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with switch Multiway Branching Fallthrough is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • The `switch` statement is a multi-way branch on an **integer** value (or enum).
  • Missing `break` causing unintended fall-through - this is one of the most common C bugs. Always end each case with `break` unless intentional fall-through is explicitly documented with a comment.
  • Using `switch` on a float or string - switch works only on integer types (int, char, enum, short, long). For floating-point or string comparisons, use if/else chains.
  • Not having a `default` case - if the switch value doesn't match any case and there's no default, execution silently skips the whole switch. Always include default to catch unexpected values.

Topics in This Module

0/8 done