Skip to content
StudyFame

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

  1. Is 76 ≥ 90? No → go down.
  2. Is 76 ≥ 80? No → go down.
  3. Is 76 ≥ 70? Yes → print "Grade: B" and stop. The remaining conditions are not checked.
  4. 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 if is 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 >= 40 first, 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 else is 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

MistakeWhat happensFix
Checking marks >= 40 before marks >= 90High marks get the wrong gradeStart from the highest value
Writing separate ifs instead of else ifMany blocks can run (95 prints A+, A, B...)Use else if
elseif or elifSyntax errorWrite else if (two words)
Forgetting the final else for invalid inputWrong values give no messageAdd an else for "invalid"

else-if Ladder Practice Questions

  1. What is an else-if ladder? When is it used?
  2. How many blocks of an else-if ladder can run?
  3. Why does the order of conditions matter?
  4. Is the final else compulsory?
  5. What is the difference between an else-if ladder and multiple separate if statements?
Show answers
  1. A chain of if / else if / else used to choose one option from many conditions.
  2. At most one (exactly one if there is a final else).
  3. 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.
  4. No, it is optional.
  5. 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)

  1. In an else-if ladder, which block runs?

    A) All true blocks   B) The first true block   C) The last true block   D) None

  2. With the grade program above, what grade does 80 get?

    A) A+   B) A   C) B   D) C

  3. The final else in a ladder is:

    A) Compulsory   B) Optional   C) Not allowed   D) Must have a condition

  4. Which is correct syntax?

    A) elseif   B) elif   C) else if   D) else-if

  5. Using the bill rates above, what is the bill for 150 units?

    A) 750   B) 850   C) 1050   D) 1500

Show answers
  1. B: the first true block.
  2. B: A (80 ≥ 90 is false, 80 ≥ 80 is true).
  3. B: optional.
  4. C: else if
  5. 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 ladderChooses one option from many conditions
Order of checkingTop to bottom
Blocks executedOnly the first true one
Final elseOptional; runs if nothing is true
For ≥ rangesStart with the highest value
Spellingelse 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.