devnotes.

if/else and switch Statements

C branches execution with if/else if/else, the ternary operator, and switch.

int hour = 14;
if (hour < 12) {
    printf("Morning\n");
} else if (hour < 18) {
    printf("Afternoon\n");
} else {
    printf("Evening\n");
}

const char *label = (hour < 12) ? "AM" : "PM"; // ternary operator

switch (hour) {
    case 0:
    case 12:
        printf("Noon or midnight\n");
        break;
    default:
        printf("Some other hour\n");
}

Any expression that evaluates to non-zero is treated as "true" in an if condition — there's no requirement that it be a comparison, which is why if (ptr) is idiomatic for checking a pointer isn't NULL.

switch only works with integer-family types (int, char, enum) — not floats or strings — and compares using exact equality against constant case values known at compile time. Forgetting break causes execution to fall through into the next case, continuing until a break or the end of the switch — sometimes intentional (stacking case 0: and case 12: above to share one action), but usually an accidental bug.

C if/else and switch Statements Explained | DevNotes