C else-if Statement

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is else if?

else if extends if / if-else into a ladder: test more conditions when earlier ones were false. Only the first matching branch runs. Written as two words: else if (not elseif).

Remember
if (cond1) {
    /* first true wins */
} else if (cond2) {
    /* only if cond1 was false */
} else if (cond3) {
    /* only if both above failed */
} else {
    /* optional default */
}

order matters  → specific checks first
vs many ifs    → else if skips the rest after a match
UsePrefer
Ranges / bands (grades, ages)else if ladder
One value vs many constantsswitch
Conditions that can all be trueSeparate if statements

How to Use else if in C

Build a top-to-bottom chain; stop at the first true condition.

  1. Start with if (first_condition).
  2. Add else if (next_condition) for each extra exclusive case.
  3. Put more specific tests higher (e.g. >= 90 before >= 80).
  4. End with optional else for the “none matched” case.
PieceRequired?Role
if (...)YesFirst test in the chain
else if (...)1+ as neededNext test when previous were false
Final elseOptionalDefault when nothing matched

Pseudocode

Pseudocode
score = some number

if score >= 90:
    grade = A
else if score >= 80:
    grade = B
else if score >= 70:
    grade = C
else if score >= 60:
    grade = D
else:
    grade = F

print grade
// only one branch runs

Cheat Sheet

Keep these facts handy while chaining conditions.

TopicDetail
Spellingelse if (two words)
RuleFirst true branch wins; rest skipped
OrderSpecific → general (top to bottom)
Final elseOptional catch-all
vs many ifsSeparate ifs can all fire; ladder is exclusive
vs switchRanges → else if; constants → switch
Compilegcc file.c -std=c11 -o out

Live Preview

Simulate an else if ladder in the browser. Same idea as the C examples below.

Browser simulation of an else if chain (same idea as the C examples below).

Result
score = 85
>= 90? false
>= 80? true → Grade: B
(rest skipped)

Worked Walkthrough — Letter Grade

Trace score = 85 through a grade ladder. Order is everything.

Trace
score = 85

if score >= 90  → false (skip A)
else if score >= 80 → true  → Grade: B
else if score >= 70 → not tested
else if score >= 60 → not tested
else                → not tested

Why order matters:
  If you tested >= 80 before >= 90,
  a score of 95 would wrongly get B.

C Examples

Compile every example with gcc file.c -std=c11 -o out. Short programs you can copy and run.

Example 1 — Letter Grade from Score

Map a numeric score to A–F. Put higher thresholds first.

C
#include <stdio.h>

int main(void) {
    int score = 85;

    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");
    }

    return 0;
}

How It Works

  1. Test A. 85 >= 90 is false.
  2. Test B. 85 >= 80 is true → print Grade: B.
  3. Skip the rest. C, D, and F are not evaluated.
  4. Order tip. Always check stricter bands before wider ones.

Example 2 — Positive, Negative, or Zero

Three exclusive outcomes with a final else for zero.

C
#include <stdio.h>

int main(void) {
    int num = 0;

    if (num > 0) {
        printf("Positive\n");
    } else if (num < 0) {
        printf("Negative\n");
    } else {
        printf("Zero\n");
    }

    return 0;
}

How It Works

  1. Positive? 0 > 0 is false.
  2. Negative? 0 < 0 is false.
  3. Default. The final else handles zero.
  4. Exclusive. Exactly one of the three messages prints.

Example 3 — Temperature Category

Classify Celsius into cold, mild, or hot using range checks.

C
#include <stdio.h>

int main(void) {
    int celsius = 28;

    if (celsius < 10) {
        printf("%d C: Cold\n", celsius);
    } else if (celsius <= 25) {
        printf("%d C: Mild\n", celsius);
    } else {
        printf("%d C: Hot\n", celsius);
    }

    return 0;
}

How It Works

  1. Cold? 28 < 10 is false.
  2. Mild? 28 <= 25 is false.
  3. Hot. The final else covers everything above 25.
  4. Ranges. Each band is exclusive because of the ladder—no overlap at runtime.

Edge Cases & Pitfalls

Check these before building long condition chains.

order

Wrong order → wrong branch

Testing >= 80 before >= 90 makes every high score look like a B. Always put narrower ranges first.

vs if

Separate ifs are not exclusive

Multiple standalone ifs can all run. Use else if when only one outcome should apply.

spelling

elseif is wrong

In C it is two keywords: else if. elseif will not compile.

switch

Long constant menus

For many == checks on one integer, switch is usually clearer than a long ladder.

Performance Notes

ApproachCostBest for
else if ladderStops at first matchRanges and exclusive cases
Many separate ifsAll conditions may runIndependent checks that can all apply
switch on constantsOften jump-table friendlyMany equal-compare options

For short ladders, clarity beats micro-optimization. Prefer switch or a lookup when the chain grows long and values are discrete.

Key Takeaways

  • Role: chain exclusive conditions; first match wins.
  • Order: specific checks before general ones.
  • else: optional default when nothing matched.
  • Choose: ranges → else if; constants → switch.

One line: else if picks the first true branch in order—write the ladder carefully.

Frequently Asked Questions

else if adds another condition to test when all previous if and else if conditions were false. Syntax: if (a) { } else if (b) { } else { }. Only the first matching branch runs.
With else if, once a branch matches, the rest are skipped. Separate if statements each run their own test independently—even after one already matched. Use else if for mutually exclusive cases.
Yes. Conditions are tested top to bottom. Put more specific checks first. In grade scoring, test >= 90 before >= 80, or a score of 95 would match the wrong branch.
No. You can end with else if only. The final else is a catch-all when every condition fails—useful for an F grade or unknown input default.
Use switch when comparing one variable against many constant values (menu options, day numbers). Use else if for ranges (score >= 80) or complex expressions.
C does not limit the count, but long chains become hard to read. For many constants, prefer switch. Refactor very long ladders into functions or lookup tables.

Next: nested if

Learn how to place conditions inside other conditions.

nested if tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful