Switch Statement in C

Level 3153 keystrokes

Uses a switch statement to match a day number to its name.

C code

// The day number to look up
1int day = 3;
// switch compares day with each case label and jumps to the one that matches
2switch (day) {
// break leaves the switch; without it, execution would fall through into the next case
3 case 1: printf("Mon\n"); break;
4 case 2: printf("Tue\n"); break;
// day is 3, so this case runs and prints Wed
5 case 3: printf("Wed\n"); break;
// default runs when no case matches
6 default: printf("Other\n");
7}

Output

Wed
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • ;× 8
  • "× 8
  • (× 5
  • )× 5
  • :× 4
  • \× 4
  • {× 1
  • }× 1
  • =× 1

More C examples at level 3

← C typing practice