Lesson 8 / 38

If ... Else

Branch with if, else if, else, the ternary operator and switch, and avoid missing-break bugs.

if / else if / else

Chains are checked top to bottom; the first true branch wins.

if (score >= 90) {
    printf("A\n");
} else if (score >= 80) {
    printf("B\n");
} else {
    printf("C or below\n");
}

Ternary

int max = (a > b) ? a : b; — a compact if/else that produces a value.

switch

switch matches one value against discrete cases. Forgetting break causes fall-through to the next case.