The else if chain lets you test multiple conditions in order and run only the first match. Think grade letters, sign checks (positive/negative/zero), or temperature bands—when more than two outcomes exist, if / else if / else is the standard C pattern.
01
Ladder
if else if.
02
First win
One branch.
03
Order
Top down.
04
else
Default.
05
Ranges
>= scores.
06
switch
Alt pattern.
Fundamentals
Definition and Usage
An else if ladder extends if-else with additional tests. C checks the first if; if false, it checks each else if in order until one is true. If none match, an optional final else runs.
Use this when outcomes are mutually exclusive—only one branch should run. That differs from stacking separate if statements, which can each fire independently.
💡
Beginner Tip
Order conditions from most specific or highest threshold to lowest. For grades, test >= 90 before >= 80, or every high score would match the first passing branch incorrectly.
Foundation
📝 Syntax
General form of an if / else if / else ladder:
C
if (condition1) {
// first match
} else if (condition2) {
// second match
} else if (condition3) {
// third match
} else {
// default when all above are false
}
How evaluation works
if — tested first.
else if — tested only if all earlier conditions were false.
else — runs only if every condition above was false.
After one branch runs, the entire ladder is finished.
Compile
C
gcc program.c -std=c11 -o program
Cheat Sheet
⚡ Quick Reference
Pattern
Example
Grade bands
if (s>=90) ... else if (s>=80) ...
Sign test
if (n>0) ... else if (n<0) ... else ...
Menu choice
if (c==1) ... else if (c==2) ...
Default catch
Final else { invalid }
vs switch
Ranges → else if; many constants → switch
Exclusive
one branch
Only first match
Order
top first
Matters
else
optional
Default
Spelling
else if
Two words
Hands-On
Examples Gallery
Compile with gcc example.c -std=c11 -o example. Each program demonstrates a multi-branch else if ladder.
📚 Getting Started
Classic grade and sign ladders from the reference material.
Example 1 — Letter Grade from Score
Assign A through F using ordered thresholds.
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
85 >= 90 is false, so C tries >= 80 next—true, prints B, and skips C, D, and F.
Example 2 — Positive, Negative, or Zero
Three-way classification 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
Both > 0 and < 0 fail for zero, so the final else handles the remaining case.
📈 Practical Patterns
Ranges, menus, and mixed nesting.
Example 3 — Temperature Category
Map Celsius readings to cold, mild, or hot.
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
Ranges overlap in thought but not in execution—only the first true branch runs. 28 skips cold and mild.
Example 4 — Simple Menu Actions
Compare integer choices with == (switch is also common here).
C
#include <stdio.h>
int main(void) {
int choice = 2;
if (choice == 1) {
printf("New file\n");
} else if (choice == 2) {
printf("Open file\n");
} else if (choice == 3) {
printf("Save file\n");
} else {
printf("Unknown option %d\n", choice);
}
return 0;
}
📤 Output:
Open file
How It Works
The final else catches invalid menu input. For many equal-compare options, see the switch tutorial.
Example 5 — else if with Nested if
Outer ladder for sign; inner if-else for even/odd positives.
C
#include <stdio.h>
int main(void) {
int num = 15;
if (num > 0) {
if (num % 2 == 0) {
printf("Positive and even.\n");
} else {
printf("Positive and odd.\n");
}
} else if (num < 0) {
printf("Negative.\n");
} else {
printf("Zero.\n");
}
return 0;
}
📤 Output:
Positive and odd.
How It Works
The first branch handles all positives with a nested split; else if and else cover negative and zero. See nested if for more.
Applications
🚀 Common Use Cases
Grading — map numeric scores to letter bands.
Classification — BMI, age groups, price tiers.
Validation — acceptable / warning / error levels.
Menus — route numeric choices to actions.
State machines — small discrete status values.
Defaults — final else for unknown input.
🧠 How an else if Ladder Works
1
Test if
Evaluate the first condition.
Start
2
Match?
Run body and exit ladder, or try next else if.
Chain
3
Repeat
Each else if tests only if all above failed.
Order
=
➜
else or done
Default branch, or continue after ladder.
Important
📝 Notes
In source code, write else if as two words (not elseif).
Only the first true branch executes; later branches are skipped.
Put higher thresholds first in range ladders (>= 90 before >= 80).
Separate if statements are not the same as else if.
A final else is optional but good for defaults.
Keep ladders readable—consider switch for many equal compares.
Performance
⚡ Optimization
Put the most common or cheapest-to-fail conditions first when order allows. Avoid redundant tests already ruled out by earlier branches. For long constant menus, a switch may compile to a jump table; for score ranges, else if is the natural fit.
Wrap Up
Conclusion
The else if ladder is how C handles three or more exclusive outcomes. Order your tests carefully, brace every branch, and add a final else when you need a default.
You now have if, if-else, and else if—combine them with nesting for richer logic.
Order conditions from specific/high to general/low
Use braces on every branch
Add else for unexpected/default cases
Keep each condition simple and readable
Use switch for many == comparisons
❌ Don’t
Reverse grade thresholds (>= 60 before >= 90)
Chain dozens of branches without refactoring
Mix separate ifs when branches are exclusive
Duplicate work in every branch
Forget that only one branch runs
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about else if
Multi-way decisions in C.
5
Core concepts
📈01
Ladder
if else if.
Syntax
✅02
One wins
Exclusive.
Flow
🔢03
Order
Top down.
Grades
📝04
else
Default.
Catch-all
🔌05
switch
Alt tool.
Menus
❓ 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.
Did you know?
else if is not a separate keyword in C—it is an else branch containing another if statement. That is why you write two words and why the final semicolon after a full ladder belongs only to any standalone statements that follow.