Celse-if Ladder

else-if Ladder

When a decision has more than two possible outcomes, chainingelse if clauses lets you test a sequence of conditions in order. This pattern is often called an else-if ladder because each rung is checked only if every rung above it failed.
Basic Syntax

C
#include <stdio.h>

int main(void) {
    int score = 72;

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

    return 0;
}
Evaluation Order Matters
Conditions in an else-if ladder are tested strictly from top to bottom. The moment one evaluates to true, its block runs and every remaining else if and else in the chain is skipped — even if a later condition would also have been true. This means ordering is a real design decision, not a stylistic choice.

C
int score = 95;

// BUGGY ORDER: a score of 95 is also >= 60, but that check never runs
// because the very first (and unrelated) branch below happens to match.
if (score >= 0) {
    printf("Just a pass\n");   // this runs first and wins
} else if (score >= 90) {
    printf("Grade: A\n");      // never reached
}
Note
Order conditions from the most specific / narrowest to the most general / broadest when ranges overlap, so that a specific case is not accidentally swallowed by a looser check placed earlier in the chain.
Ladder vs. Independent if Statements
An else-if ladder is not the same as a series of separate, independent if statements. In a ladder, only one branch ever executes. With independent ifs, every condition is evaluated on its own, so multiple blocks can run in the same pass.

C
int n = 10;

// Ladder: only ONE branch runs.
if (n % 2 == 0) {
    printf("even\n");
} else if (n % 5 == 0) {
    printf("multiple of 5\n"); // skipped, even though it's also true
}

// Independent ifs: BOTH conditions are checked and BOTH can run.
if (n % 2 == 0) {
    printf("even\n");
}
if (n % 5 == 0) {
    printf("multiple of 5\n"); // this also prints
}
When the Ladder Gets Unwieldy
Else-if ladders work well for a handful of branches, especially when conditions involve ranges or multiple different variables. But once you are checking many possible exact values of a single integer or character variable, a long ladder becomes hard to scan and easy to get wrong. In that specific situation, C's switch statement is usually a cleaner fit — it is designed exactly for "compare one value against a list of exact matches" and reads more like a menu than a chain of comparisons.

C
// Getting unwieldy: many branches all comparing the SAME variable
// to exact values -- a good candidate for a switch statement instead.
if (day == 1) {
    printf("Monday\n");
} else if (day == 2) {
    printf("Tuesday\n");
} else if (day == 3) {
    printf("Wednesday\n");
} else if (day == 4) {
    printf("Thursday\n");
} else if (day == 5) {
    printf("Friday\n");
} else {
    printf("Weekend\n");
}
Tip
Reach for a switch when you are testing one variable against several exact constant values. Keep the if / else if ladder when conditions involve ranges, comparisons, or multiple variables — things a switch cannot express directly.
Key Points
  • else-if ladders are evaluated top to bottom; the first true condition runs and the rest are skipped.

  • Order matters when ranges overlap -- put more specific conditions before more general ones.

  • A ladder is different from separate if statements: a ladder runs at most one branch, independent ifs can run several.

  • When comparing one variable against many exact values, consider a switch statement instead of a long ladder.