Loading...

C essentialsControl Flow

The switch statement

7 min lesson

Next
  • A switch statement chooses one path from many based on the exact value of a single integer or character expression.
  • switch matters because it is cleaner and easier to read than a long chain of else if when you compare one value against many fixed cases.
  • Think of a switch as a train track junction: the value decides which track the train takes, and break stops the train at the end of that track.
  • A case labels a possible value, like case 1:, and the code after it runs when the switch value matches.
  • break exits the switch. Without it, execution falls through into the next case, which is sometimes useful but usually a bug.
  • The default case runs when no other case matches and is a good place to report an unexpected value.
  • The computer compares the switch value to each case in order and jumps to the first match.
  • The syntax is: switch (value) { case 1: ... break; default: ... break; }.
  • A realistic use: mapping a menu number to an action, or turning a digit into its English word.
  • A surprising detail: switch only works on integer types (including char), not on floats or strings.
  • The example program prints the day name for a day number using switch, with a default for invalid numbers.
Keep in mind
  • Always add break after each case unless you intentionally want fall-through.
  • Include a default case to handle unexpected values.
  • Use switch only for exact integer or char matches, not for ranges.
  • Comment intentional fall-through so readers know it is not a bug.
  • switch makes many-way choices easy to read. Well done!
main.c
#include <stdio.h>

int main(void) {
    // A day number from 1 to 7
    int day = 3;

    // Choose the day name based on the number
    switch (day) {
        case 1:
            printf("Monday\n");
            break; // stop here; do not fall into case 2
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        case 4:
            printf("Thursday\n");
            break;
        case 5:
            printf("Friday\n");
            break;
        case 6:
            printf("Saturday\n");
            break;
        case 7:
            printf("Sunday\n");
            break;
        default:
            // Runs only when day is not 1 to 7
            printf("Invalid day\n");
            break;
    }

    return 0;
}

What happens: Wednesday

Breaking it down

Switch expression

switch (day)

The value inside the parentheses is compared against each case label. It must be an integer or char, not a float or string.

Case label

case 3:

Marks a possible value. When day equals 3, execution jumps here and runs the lines below until a break.

break

break;

Leaves the switch so the next case is not run. Forgetting it makes execution fall through into the next case.

default case

default:

Catches any value that matched no case. It is optional but recommended so unexpected values are handled.

A helpful analogy

A switch is like a junction on a train track. The train (the value) arrives, and the switch sends it down exactly one track (the matching case). The break is the buffer at the end of the track that stops the train from rolling into the next one.

Nesting rules

  • 1.Every case must end with break unless you deliberately want fall-through to the next case.
  • 2.Case labels must be constant integer or char values; variables are not allowed.
  • 3.Only one default is allowed per switch, and it can appear anywhere, but putting it last is clearest.
  • 4.The switch expression must be an integer type; floats and strings cannot be used.

Common mistakes

Forgetting break

Avoid

case 1: printf("one"); case 2: printf("two");

Use instead

case 1: printf("one"); break;

Without break, execution continues into the next case and prints both. This is called fall-through and is rarely what you want.

Using a variable as a case label

Avoid

int x = 3; case x: // error

Use instead

case 3: // constant

Case labels must be compile-time constants. Use if-else if you need to compare against a variable.

Trying to switch on a float or string

Avoid

switch (3.14) { ... } // error

Use instead

if (x == 3.14) { ... }

switch only accepts integer types. For floating-point or string comparisons, use if-else.

Quick tips

  • •Put the most common cases first; the order only matters for readability, not speed.
  • •Use a comment like // fall through when you intentionally skip a break.
  • •Try changing day in the example to 8 to see the default case run.
  • •Group cases that share the same code by stacking labels: case 6: case 7:.
  • •Let the compiler warn about missing break with -Wimplicit-fallthrough.
Previous
Next