typestar

switch statements in C

Multi-way branching with fall-through cases.

int days_in_month(int month) {
    switch (month) {
        case 2:
            return 28;
        case 4: case 6: case 9: case 11:
            return 30;
        default:
            return 31;
    }
}

How it works

  1. switch jumps to the matching case label.
  2. Stacked labels share one body.
  3. default handles everything else.

Keywords and builtins used here

The run, in numbers

Lines
10
Characters to type
134
Tokens
40
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 28 seconds.

Type this snippet

Step 5 of 7 in Operators & flow, step 12 of 35 in Language basics.

← Previous Next →