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
Build a top-to-bottom chain; stop at the first true condition.
Start with if (first_condition).
Add else if (next_condition) for each extra exclusive case.
Put more specific tests higher (e.g. >= 90 before >= 80).
End with optional else for the “none matched” case.
Piece
Required?
Role
if (...)
Yes
First test in the chain
else if (...)
1+ as needed
Next test when previous were false
Final else
Optional
Default 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
Reference
Cheat Sheet
Keep these facts handy while chaining conditions.
Topic
Detail
Spelling
else if (two words)
Rule
First true branch wins; rest skipped
Order
Specific → general (top to bottom)
Final else
Optional catch-all
vs many ifs
Separate ifs can all fire; ladder is exclusive
vs switch
Ranges → else if; constants → switch
Compile
gcc file.c -std=c11 -o out
Try it
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).
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.
Code
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;
}
Output
Grade: B
How It Works
Test A.85 >= 90 is false.
Test B.85 >= 80 is true → print Grade: B.
Skip the rest. C, D, and F are not evaluated.
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;
}
Output
Zero
How It Works
Positive?0 > 0 is false.
Negative?0 < 0 is false.
Default. The final else handles zero.
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;
}
Output
28 C: Hot
How It Works
Cold?28 < 10 is false.
Mild?28 <= 25 is false.
Hot. The final else covers everything above 25.
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.
Analysis
Performance Notes
Approach
Cost
Best for
else if ladder
Stops at first match
Ranges and exclusive cases
Many separate ifs
All conditions may run
Independent checks that can all apply
switch on constants
Often jump-table friendly
Many equal-compare options
For short ladders, clarity beats micro-optimization. Prefer switch or a lookup when the chain grows long and values are discrete.
Remember
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.