Switch tracer Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

The ideas behind the lab

Exactly what you watched happen, in plain steps.

The shape of a switch

switch takes ONE whole-number value (an int, a char …) and jumps straight to the case label with the same value. From there the statements run until a break leaves the switch. If no label matches, it jumps to default — or skips the whole switch if there is no default.

switch (expression) {
    case 1:
        statements;
        break;          // leave the switch
    case 2:
    case 3:             // 2 and 3 share this block
        statements;
        break;
    default:            // no case matched
        statements;
}
switch or else-if ladder?
switch else-if ladder
Tests one value against fixed constants any conditions
Types whole numbers and characters only anything (double, ranges, several variables)
Ranges like x > 5 no yes
How it decides jumps straight to the matching case checks the conditions from the top
Best for menus, commands, codes, letters ranges, mixed conditions

switch and case

  1. The expression in switch ( ) is worked out once; it must be a whole number (int, char, long …).
  2. case labels are constants: case 1:, case 'a':, case MAX: (a #define). Note the colon.
  3. switch JUMPS to the matching label — the cases above it are not run.
  4. Every label must be different.
  5. Use a switch when one value chooses among many fixed options.

break and fall-through

  1. break leaves the switch immediately.
  2. Without break, execution FALLS THROUGH into the next case, ignoring its label.
  3. Forgetting break is the most common switch bug (gcc -Wextra: -Wimplicit-fallthrough).
  4. Stacked labels (case 'a': case 'e': …) share one block — fall-through on purpose.
  5. Mark intentional fall-through with a comment: /* fall through */.

default and the rules

  1. default runs when no case matches; it is optional but recommended.
  2. Without default and without a match, the switch silently does nothing.
  3. default can be anywhere, but it is usually last (then it needs no break).
  4. No doubles, no strings, no variables, no ranges as case labels.
  5. To declare a variable inside a case, put the case's statements in { }.
Walkthrough

A tiny calculator

Every line, explained.

1 #include <stdio.h>
2
3 int main(void)
4 {
5 double a = 12, b = 4;
6 char op = '/'; The operator is a char — a small whole number, so it can be switched on.
7 switch (op) { Jump to the case for '/'.
8 case '+': printf("%.2f\n", a + b); break;
9 case '-': printf("%.2f\n", a - b); break;
10 case '*': printf("%.2f\n", a * b); break;
11 case '/': This is the matching case.
12 if (b != 0) A case can contain any statements — even an if.
13 printf("%.2f\n", a / b); Prints 3.00
14 else
15 printf("Cannot divide by zero\n");
16 break; Leave the switch.
17 default: Any other character lands here.
18 printf("Unknown operator %c\n", op);
19 }
20 return 0;
21 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

A forgotten break

✗ Wrong
case 1: puts("Tea");
case 2: puts("Coffee"); break;
✓ Right
case 1: puts("Tea"); break;
case 2: puts("Coffee"); break;

Without break, choice 1 prints Tea AND Coffee: execution falls through into case 2.

A condition as a case label

✗ Wrong
switch (marks) {
    case marks >= 90: puts("A");
}
✓ Right
if (marks >= 90)
    puts("A");

Labels must be constants. Ranges and conditions need if / else if.

A semicolon instead of a colon

✗ Wrong
case 1; puts("one"); break;
✓ Right
case 1: puts("one"); break;

A case label ends with a colon. The semicolon gives a compiler error: expected ':' before ';'.

Declaring a variable right after a label

✗ Wrong
case 1:
    int total = 5;
    break;
✓ Right
case 1: {
    int total = 5;
    break;
}

Before C23, a label cannot be followed by a declaration: "a label can only be part of a statement". Braces make a block.

switch on a string

✗ Wrong
switch (answer) {
    case "yes": …
}
✓ Right
switch (answer[0]) {
    case 'y': …
}

switch works only with whole numbers. Compare strings with strcmp (in the Strings part), or switch on one character.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

int x = 2; switch (x) { case 1: puts("A"); break; case 2: puts("B"); break; default: puts("C"); } — output?

Question 2

The same switch WITHOUT any break and x = 2 — output?

Question 3

Which type can NOT be used in switch ( )?

Question 4

When does default run?

Question 5

Which is a valid case label?

Assessment

switch Statement quiz

Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.