C Workshop · Switch Day Number
Lesson C

Switch Day Number

switch jumps to the matching case label. Always break so you do not fall through.

What it is

switch is a jump table. You hand it an integer (or a char), and it leaps to the matching case label. It is the right tool when you have exact values — Monday is 1, not “greater than 0.”

Approach

  1. Put the integer in switch (d).
  2. Write a case for each value you care about.
  3. Print, then break.
  4. Add default for anything else.

Where this is used in the real world

Diagram

1

d=3

selector

2

case 1

skip

3

case 2

skip

4

case 3

Wed

Run algorithm

Each step highlights the matching C line. Program output appears only when a line that prints actually runs.

Ready
C code
1#include <stdio.h>
2int main(void) {
3    int d = 3;
4    switch (d) {
5        case 1: printf("Mon\n"); break;
6        case 2: printf("Tue\n"); break;
7        case 3: printf("Wed\n"); break;
8        default: printf("?\n"); break;
9    }
10    return 0;
11}
What this line does
Press Run algorithm. The highlight will move through the C code.
True branch
the if body runs
False branch
else / skip
Program output
(no output yet)

Check your understanding

Five questions. Pick one answer each, then Check answers. The correct choice turns green.

1. d = 3 prints:

case 3.

2. What does break do here?

Prevent fall-through.

3. default runs when:

Leftover.

4. Missing break after case 3 would:

Fall-through.

5. switch cannot directly test:

Need if for ranges.