{ }codelper

Reference › C

C switch Statement: case, break, default

switch compares one integer value with the constants of several case labels, jumps to the one that is equal, and runs from there. When it meets break, it leaves the switch. It is easier to read when if (x == 1) ... else if (x == 2) ... gets long.

switch (integer expression) {
case constant1:
    statements
    break;
case constant2:
    statements
    break;
default:
    when no case is equal
}

case, break, default

It finds the case equal to the value in the parentheses and runs from that line. If no case matches, it goes to default. You can leave default out, and then it does nothing and moves on.

#include <stdio.h> int main(void) {    int rank;    scanf("%d", &rank);    switch (rank) {    case 1: printf("gold\n"); break;    case 2: printf("silver\n"); break;    case 3: printf("bronze\n"); break;    default: printf("no medal\n");    }    return 0;}
Input
2
Output
silver

Edit and run it →

Grouping several cases

A case is only a label that says "start here", so without a break execution flows straight down into the statements of the next case. You can use this to group values that do the same thing, by writing just their case labels one after another.

#include <stdio.h> int main(void) {    int month, days;    scanf("%d", &month);    switch (month) {    case 2:        days = 28; break;    case 4: case 6: case 9: case 11:        days = 30; break;    default:        days = 31;    }    printf("%d\n", days);    return 0;}
Input
9
Output
30

Edit and run it →

Switching on a character, and break inside a loop

A char is an integer too, so you can switch on characters, as in case '+':. One thing to watch: a break inside a switch leaves only the switch. To end the for or while around it as well, use a flag variable as below.

#include <stdio.h> int main(void) {    char cmd[] = "++-q++";    int value = 0, running = 1;    for (int i = 0; cmd[i] != '\0' && running; i++) {        switch (cmd[i]) {        case '+': value++; break;        case '-': value--; break;        case 'q': running = 0; break;        }    }    printf("%d\n", value);    return 0;}
Output
1

Edit and run it →

When it meets q, running becomes 0 and the loop ends at the condition of the for. So the ++ after q is not counted.

What switch can't do

The value you give to switch must be an integer, such as int, char or long long, and a case can only have a constant that is fixed at compile time, such as 3 or 'a'.

So switch can't do floating-point numbers (double), strings, range conditions such as score >= 90, or comparing with a variable. Use if and else if for these. You also can't write the same constant in two case labels.

Common mistakes

Leaving out break

#include <stdio.h>

int main(void) {
    int n = 1;
    switch (n) {
    case 1: printf("one\n");
    case 2: printf("two\n");
    default: printf("other\n");
    }
    return 0;
}

→ Three lines: not only one, but two and other as well

It starts at case 1 and, with no break, flows down to the end. Write break; at the end of every case, unless you left it out on purpose to group cases.

Putting a string in switch

char cmd[10] = "go";
switch (cmd) {          /* a string is not an integer */
case "go": printf("start\n"); break;
}

→ It doesn't compile

Strings have to be compared with strcmp, so write if (strcmp(cmd, "go") == 0) ... else if .... If the first character alone is enough to tell them apart, switch (cmd[0]) works too.

Writing a range condition in a case

switch (score) {
case score >= 90: printf("A\n"); break;   /* not a constant */
case score >= 80: printf("B\n"); break;
}

→ It doesn't compile

Only a constant can come after case. Write ranges as if (score >= 90) ... else if (score >= 80) .... If the ranges split exactly every 10 points, you can also use switch (score / 10) with case 10: case 9:.

Practice problems

See also