Conditionals

Every language needs a way to run different code depending on a condition. C's version — if, else, and switch — will look familiar if you've used almost anything else, with one important wrinkle: C considers any nonzero value true and exactly zero false, without ever needing an actual boolean type.

if / else if / else

Conditions are checked top to bottom, and the first one that's true wins — everything after it is skipped:

</> grading.c
#include <stdio.h>

int main(void) {
    int score = 82;

    if (score >= 90) {
        printf("Grade: A\n");
    } else if (score >= 80) {
        printf("Grade: B\n");
    } else if (score >= 70) {
        printf("Grade: C\n");
    } else {
        printf("Grade: F\n");
    }

    return 0;
}
Output
Grade: B

Even though score is also >= 70, that branch never gets checked — the moment score >= 80 matches, C runs that block and jumps past the rest of the chain.

switch, and the case for break

A switch statement compares one value against a list of possible matches. What surprises newcomers is that C doesn't automatically stop after running the matching case — it keeps executing every statement below it until it hits a break or runs out of cases. This is called fall-through, and while it's occasionally useful on purpose, forgetting a break is one of the most common switch-statement bugs:

</> weekday.c
#include <stdio.h>

int main(void) {
    int day = 3;

    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
        case 4:
            printf("Thursday\n");
            break;
        default:
            printf("Some other day\n");
    }

    return 0;
}
Output
Wednesday
Thursday

With day equal to 3, execution lands on case 3, prints "Wednesday", and — since there's no break there — keeps going straight into case 4's printf before finally hitting a break. The default label never runs, and it never would have even if none of the cases matched anything less than 3, because default only fires when nothing above it matched at all.

Note: if the missing break above looked like a mistake, that's the point — it's the single most common bug in switch statements. Every case should end in break (or return) unless you deliberately want it to fall into the next one.

The ternary operator

For a simple either/or choice, condition ? valueIfTrue : valueIfFalse can replace a whole if/else block with a single expression:

</> ternary.c
#include <stdio.h>

int main(void) {
    int temperature = 55;
    const char *label = (temperature > 60) ? "warm" : "cool";

    printf("It feels %s today.\n", label);
    return 0;
}
Output
It feels cool today.

%s tells printf to print a string — you'll see exactly what const char * means and why strings work this way in lesson 9. For now, just note that the ternary operator is an expression, not a statement, so it always has to produce a value on both sides of the :.