CodeMIRAI
0 XP

switch

2 min read 5 runnable examples Quiz included

When one value decides between many fixed options, switch is tidier than a long chain of else ifs:

C
#include <stdio.h>

int main() {
    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("Some other day\n");
    }
    return 0;
}

switch jumps to the case whose value matches. break leaves the switch, and default runs when no case matches.

Fall-through

C
#include <stdio.h>

int main() {
    int day = 2;
    switch (day) {
        case 1:
            printf("Monday\n");
        case 2:
            printf("Tuesday\n");
        case 3:
            printf("Wednesday\n");
        default:
            printf("Some other day\n");
    }
    return 0;
}

Without break, execution doesn't stop at the next case label: it falls through and runs every case below the match. This prints Tuesday, Wednesday and Some other day. A forgotten break is the most common switch bug.

Grouping cases

Fall-through is useful on purpose, when several values share the same code:

C
#include <stdio.h>

int main() {
    char grade = 'B';
    switch (grade) {
        case 'A':
        case 'B':
            printf("Great work\n");
            break;
        case 'C':
            printf("Good\n");
            break;
        case 'D':
        case 'E':
            printf("You passed\n");
            break;
        default:
            printf("Let's try again\n");
    }
    return 0;
}

A char is a small integer in C, so it works in a switch too.

A menu

This example's input is 2 12 4: option 2, then two numbers.

C
#include <stdio.h>

int main() {
    int choice, a, b;
    printf("1) add   2) multiply   3) subtract\n");
    scanf("%d %d %d", &choice, &a, &b);
    switch (choice) {
        case 1: printf("%d + %d = %d\n", a, b, a + b); break;
        case 2: printf("%d * %d = %d\n", a, b, a * b); break;
        case 3: printf("%d - %d = %d\n", a, b, a - b); break;
        default: printf("There is no option %d\n", choice);
    }
    return 0;
}

Counting characters

C
#include <stdio.h>

int main() {
    char text[] = "programming in c is fun";
    int vowels = 0, spaces = 0, others = 0;
    for (int i = 0; text[i] != '\0'; i++) {
        switch (text[i]) {
            case 'a': case 'e': case 'i': case 'o': case 'u':
                vowels++;
                break;
            case ' ':
                spaces++;
                break;
            default:
                others++;
        }
    }
    printf("vowels %d, spaces %d, other letters %d\n", vowels, spaces, others);
    return 0;
}

What switch can't do

  • Every case needs a constant whole number (or a character): no variables, no decimals, no ranges like "90 to 100".
  • It can't compare strings: case "Asha": doesn't compile. Compare strings with strcmp in an if (see the strings lesson).

For ranges and other conditions, if-else if is the right tool:

C
if (score >= 90)      printf("A\n");
else if (score >= 75) printf("B\n");
else                  printf("C\n");
Finished this lesson?Mark it complete to earn +20 XP and move your progress on.