else-if Ladder in C
Sometimes there are more than two choices. A grade can be A+, A, B, C, D or F. An electricity bill has different rates for different slabs. if-else gives only two paths.
The else-if ladder checks conditions one by one, from top to bottom, and runs the block of the first true condition. In this lesson you will build a grade calculator and an electricity bill program, two favourite exam questions.
What You Will Learn
- The syntax and flowchart of the else-if ladder.
- Why only the first true condition runs.
- Why the order of conditions matters.
- The grade calculator program.
- The electricity bill program with slabs.
else-if Ladder in C Explained
Exam definition: The else-if ladder is a chain of if ... else if ... else statements used to choose one option from many. Conditions are checked from top to bottom; the block of the first true condition runs and the rest are skipped. The final else runs if no condition is true.
Memory trick: Think of a ladder: you climb down step by step and stop at the first step that fits. If no step fits, you land on the ground: the final else.
Syntax of the else-if Ladder
if (condition1)
{
/* runs if condition1 is true */
}
else if (condition2)
{
/* runs if condition1 is false and condition2 is true */
}
else if (condition3)
{
/* ... */
}
else
{
/* runs if no condition is true (optional) */
}
Flowchart of the else-if ladder
< condition1? > --Yes--> [ block 1 ] --+
| No |
< condition2? > --Yes--> [ block 2 ] --+
| No |
< condition3? > --Yes--> [ block 3 ] --+
| No |
[ else block ] ----------------------+
|
[ next statement ]
Example 1: Grade Calculator Program in C
#include <stdio.h>
int main(void)
{
int marks;
printf("Enter marks (0-100): ");
scanf("%d", &marks);
if (marks >= 90)
printf("Grade: A+\n");
else if (marks >= 80)
printf("Grade: A\n");
else if (marks >= 70)
printf("Grade: B\n");
else if (marks >= 60)
printf("Grade: C\n");
else if (marks >= 40)
printf("Grade: D\n");
else
printf("Grade: F (Fail)\n");
return 0;
}
Output
Enter marks (0-100): 76 Grade: B
Code Explanation
- Is 76 ≥ 90? No → go down.
- Is 76 ≥ 80? No → go down.
- Is 76 ≥ 70? Yes → print "Grade: B" and stop. The remaining conditions are not checked.
- We don't need to write
marks >= 70 && marks < 80, because if marks were 80 or more, an earlier condition would already have matched.
How the else-if Ladder Works
- Each
else ifis checked only if all the conditions above it were false. - As soon as one is true, its block runs and C jumps to the end of the ladder.
- So the order matters. If you checked
marks >= 40first, then 95 would get grade D, because 95 ≥ 40 is true and the ladder stops there. - Rule: for "greater than or equal" checks, start from the highest value.
Example 2: Electricity Bill Program in C
Rates: first 100 units at ₹5, next 100 units (101–200) at ₹7, units above 200 at ₹10.
#include <stdio.h>
int main(void)
{
int units;
float bill;
printf("Enter units consumed: ");
scanf("%d", &units);
if (units <= 100)
bill = units * 5;
else if (units <= 200)
bill = 100 * 5 + (units - 100) * 7;
else
bill = 100 * 5 + 100 * 7 + (units - 200) * 10;
printf("Electricity bill: Rs. %.2f\n", bill);
return 0;
}
Output
Enter units consumed: 250 Electricity bill: Rs. 1700.00
250 units: first 100 × 5 = 500, next 100 × 7 = 700, remaining 50 × 10 = 500. Total = 1700.
Important Points to Remember
- Conditions are checked top to bottom.
- Only the first true block runs; the rest are skipped.
- The final
elseis optional; it handles "none of the above". - Order matters: put the most specific (highest) condition first.
- Use it when there are many ranges to check (grades, slabs, age groups).
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
Checking marks >= 40 before marks >= 90 | High marks get the wrong grade | Start from the highest value |
Writing separate ifs instead of else if | Many blocks can run (95 prints A+, A, B...) | Use else if |
elseif or elif | Syntax error | Write else if (two words) |
Forgetting the final else for invalid input | Wrong values give no message | Add an else for "invalid" |
else-if Ladder Practice Questions
- What is an else-if ladder? When is it used?
- How many blocks of an else-if ladder can run?
- Why does the order of conditions matter?
- Is the final else compulsory?
- What is the difference between an else-if ladder and multiple separate if statements?
Show answers
- A chain of if / else if / else used to choose one option from many conditions.
- At most one (exactly one if there is a final else).
- Conditions are checked top to bottom and the first true one wins, so a general condition placed early can hide the specific ones below it.
- No, it is optional.
- In a ladder, only the first true block runs and the rest are skipped; separate ifs are all checked, so several blocks can run.
Coding Exercise
Read a number and print whether it is "Positive", "Negative" or "Zero" using an else-if ladder.
Show solution
#include <stdio.h>
int main(void)
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n > 0)
printf("Positive\n");
else if (n < 0)
printf("Negative\n");
else
printf("Zero\n");
return 0;
}
Enter a number: -8 Negative
Quick Quiz: else-if Ladder in C (MCQ)
-
In an else-if ladder, which block runs?
A) All true blocks B) The first true block C) The last true block D) None
-
With the grade program above, what grade does 80 get?
A) A+ B) A C) B D) C
-
The final
elsein a ladder is:A) Compulsory B) Optional C) Not allowed D) Must have a condition
-
Which is correct syntax?
A)
elseifB)elifC)else ifD)else-if -
Using the bill rates above, what is the bill for 150 units?
A) 750 B) 850 C) 1050 D) 1500
Show answers
- B: the first true block.
- B: A (80 ≥ 90 is false, 80 ≥ 80 is true).
- B: optional.
- C:
else if - B: 100 × 5 + 50 × 7 = 500 + 350 = 850.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| else-if ladder | Chooses one option from many conditions |
|---|---|
| Order of checking | Top to bottom |
| Blocks executed | Only the first true one |
| Final else | Optional; runs if nothing is true |
| For ≥ ranges | Start with the highest value |
| Spelling | else if (two words) |
Frequently asked questions
- When should you use an else-if ladder?
- When there are many conditions or ranges and only one option should be chosen.
- Why does the order of conditions matter?
- The first true condition wins, so a broad condition placed early hides the ones below.
- else-if ladder vs separate ifs?
- A ladder stops at the first match; separate ifs check every condition.
- else-if ladder vs switch?
- else-if works with ranges and any condition; switch only matches exact constant values.
Remember
- Ladder = stop at the first step that fits.
- Highest condition first for ≥ checks.
Next Topic
Next lesson: Nested if-else in C. You will learn how to put one if inside another, and find the largest of three numbers.