Lesson 25 of 60 – else-if Ladder in C
42%

else-if Ladder in C

The else-if ladder is used when a program needs to check multiple conditions. The conditions are evaluated from top to bottom, and the first true condition is executed.

Note: In an else-if ladder, once one condition becomes true, its block is executed and the remaining conditions are skipped. If none of the conditions is true, the final else block, if present, is executed.

1. What is an else-if Ladder?

An else-if ladder allows you to test several conditions one after another. It is useful when there are more than two possible outcomes.

if (condition1)
{
    // statement
}
else if (condition2)
{
    // statement
}
else if (condition3)
{
    // statement
}
else
{
    // default statement
}

2. Basic Syntax

if (condition1)
{
    statement1;
}
else if (condition2)
{
    statement2;
}
else
{
    statement3;
}

The program first checks condition1. If it is false, it checks condition2. If all conditions are false, the else block executes.

3. Simple else-if Example

int number = 0;

if (number > 0)
{
    printf("Positive");
}
else if (number < 0)
{
    printf("Negative");
}
else
{
    printf("Zero");
}

The program checks three possible cases: positive, negative, and zero.

4. How an else-if Ladder Works

The execution takes place from top to bottom:

  1. The first if condition is checked.
  2. If it is true, its block executes.
  3. If it is false, the next else if condition is checked.
  4. This continues until a true condition is found.
  5. If no condition is true, the final else executes.

5. Checking Marks

int marks = 75;

if (marks >= 80)
{
    printf("Grade A");
}
else if (marks >= 60)
{
    printf("Grade B");
}
else if (marks >= 40)
{
    printf("Grade C");
}
else
{
    printf("Fail");
}

Since the marks are 75, the second condition is true and Grade B is displayed.

6. Grade Calculation

int marks = 92;

if (marks >= 90)
{
    printf("A+");
}
else if (marks >= 80)
{
    printf("A");
}
else if (marks >= 70)
{
    printf("B");
}
else if (marks >= 60)
{
    printf("C");
}
else if (marks >= 40)
{
    printf("D");
}
else
{
    printf("F");
}

The conditions are checked from the highest range to the lowest range.

7. Checking Age Groups

int age = 25;

if (age < 13)
{
    printf("Child");
}
else if (age < 20)
{
    printf("Teenager");
}
else if (age < 60)
{
    printf("Adult");
}
else
{
    printf("Senior Citizen");
}

Each condition represents a different age group.

8. Checking Positive, Negative and Zero

int number;

printf("Enter a number: ");
scanf("%d", &number);

if (number > 0)
{
    printf("Positive");
}
else if (number < 0)
{
    printf("Negative");
}
else
{
    printf("Zero");
}

This is a common beginner example of an else-if ladder.

9. Comparing Three Numbers

int a = 30;
int b = 20;
int c = 10;

if (a > b && a > c)
{
    printf("A is largest");
}
else if (b > a && b > c)
{
    printf("B is largest");
}
else
{
    printf("C is largest or there is a tie");
}

Logical operators can be used inside else-if conditions.

10. Checking Day Number

int day = 3;

if (day == 1)
{
    printf("Monday");
}
else if (day == 2)
{
    printf("Tuesday");
}
else if (day == 3)
{
    printf("Wednesday");
}
else
{
    printf("Invalid day");
}

Each condition checks a different value.

11. Multiple Conditions

int marks = 68;

if (marks >= 90)
{
    printf("Excellent");
}
else if (marks >= 75)
{
    printf("Very Good");
}
else if (marks >= 50)
{
    printf("Good");
}
else
{
    printf("Needs Improvement");
}

Only the first matching condition is executed.

12. else-if with User Input

#include <stdio.h>

int main()
{
    int marks;

    printf("Enter marks: ");
    scanf("%d", &marks);

    if (marks >= 90)
    {
        printf("Grade A+");
    }
    else if (marks >= 80)
    {
        printf("Grade A");
    }
    else if (marks >= 60)
    {
        printf("Grade B");
    }
    else if (marks >= 40)
    {
        printf("Grade C");
    }
    else
    {
        printf("Fail");
    }

    return 0;
}

13. Temperature Classification

int temperature = 35;

if (temperature >= 40)
{
    printf("Very Hot");
}
else if (temperature >= 30)
{
    printf("Hot");
}
else if (temperature >= 20)
{
    printf("Normal");
}
else
{
    printf("Cold");
}

The program classifies the temperature into different ranges.

14. Salary Classification

int salary = 45000;

if (salary >= 100000)
{
    printf("High Salary");
}
else if (salary >= 50000)
{
    printf("Good Salary");
}
else if (salary >= 25000)
{
    printf("Average Salary");
}
else
{
    printf("Low Salary");
}

15. Checking Divisibility

int number = 15;

if (number % 2 == 0)
{
    printf("Divisible by 2");
}
else if (number % 3 == 0)
{
    printf("Divisible by 3");
}
else if (number % 5 == 0)
{
    printf("Divisible by 5");
}
else
{
    printf("Not divisible by 2, 3 or 5");
}

Remember that the first true condition stops the ladder.

16. Checking a Character

char grade = 'B';

if (grade == 'A')
{
    printf("Excellent");
}
else if (grade == 'B')
{
    printf("Very Good");
}
else if (grade == 'C')
{
    printf("Good");
}
else
{
    printf("Needs Improvement");
}

Character values can be compared using the equality operator.

17. Simple Calculator Using else-if

char operator = '+';
int a = 10;
int b = 5;

if (operator == '+')
{
    printf("%d", a + b);
}
else if (operator == '-')
{
    printf("%d", a - b);
}
else if (operator == '*')
{
    printf("%d", a * b);
}
else if (operator == '/')
{
    printf("%d", a / b);
}
else
{
    printf("Invalid operator");
}

An else-if ladder can select an operation based on the operator entered.

18. Checking Voter Eligibility

int age = 17;

if (age < 0)
{
    printf("Invalid age");
}
else if (age >= 18)
{
    printf("Eligible to vote");
}
else
{
    printf("Not eligible to vote");
}

The first condition handles invalid input before checking eligibility.

19. Checking Profit or Loss

int cost = 1000;
int selling = 1200;

if (selling > cost)
{
    printf("Profit");
}
else if (selling < cost)
{
    printf("Loss");
}
else
{
    printf("No profit and no loss");
}

Three possible outcomes are handled using the ladder.

20. else-if with Floating-Point Values

float percentage = 82.5f;

if (percentage >= 90.0f)
{
    printf("Excellent");
}
else if (percentage >= 75.0f)
{
    printf("Very Good");
}
else if (percentage >= 60.0f)
{
    printf("Good");
}
else
{
    printf("Needs Improvement");
}

Floating-point variables can also be used in an else-if ladder.

21. Order of Conditions Matters

int marks = 85;

if (marks >= 40)
{
    printf("Pass");
}
else if (marks >= 80)
{
    printf("Excellent");
}

Here, marks >= 40 is true first, so the second condition is never reached.

For range-based grading, conditions should generally be arranged from the highest threshold to the lowest when that matches the intended logic.

22. Using Range Conditions

int marks = 72;

if (marks >= 90)
{
    printf("A+");
}
else if (marks >= 80)
{
    printf("A");
}
else if (marks >= 70)
{
    printf("B");
}
else if (marks >= 60)
{
    printf("C");
}
else
{
    printf("Fail");
}

Because the conditions are checked from top to bottom, 72 matches the marks >= 70 condition.

23. else-if Without Final else

The final else is optional.

int number = 20;

if (number > 100)
{
    printf("Greater than 100");
}
else if (number > 10)
{
    printf("Greater than 10");
}

If no condition is true, nothing inside the ladder executes.

24. Nested if vs else-if Ladder

Multiple related alternatives are often easier to read with an else-if ladder.

if (marks >= 80)
{
    printf("A");
}
else if (marks >= 60)
{
    printf("B");
}
else
{
    printf("C");
}

Nested if statements are still useful when a decision depends on another decision.

25. Common Mistake: Incorrect Ordering

One common mistake is placing a broad condition before a more specific condition.

if (marks >= 40)
{
    printf("Pass");
}
else if (marks >= 90)
{
    printf("Excellent");
}

The second condition can never be reached for values 90 or higher because those values already satisfy the first condition.

Arrange conditions carefully according to the required logic.

26. Common Mistake: Using = Instead of ==

Incorrect:

if (number = 10)
{
    printf("Ten");
}
else if (number = 20)
{
    printf("Twenty");
}

The = operator performs assignment. Use == when you want to compare values.

Correct:

if (number == 10)
{
    printf("Ten");
}
else if (number == 20)
{
    printf("Twenty");
}

27. Complete Grade Program

#include <stdio.h>

int main()
{
    int marks;

    printf("Enter marks: ");
    scanf("%d", &marks);

    if (marks >= 90)
    {
        printf("Grade A+");
    }
    else if (marks >= 80)
    {
        printf("Grade A");
    }
    else if (marks >= 70)
    {
        printf("Grade B");
    }
    else if (marks >= 60)
    {
        printf("Grade C");
    }
    else if (marks >= 40)
    {
        printf("Grade D");
    }
    else
    {
        printf("Fail");
    }

    return 0;
}

28. Complete Number Classification Program

#include <stdio.h>

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    if (number > 0)
    {
        printf("Positive");
    }
    else if (number < 0)
    {
        printf("Negative");
    }
    else
    {
        printf("Zero");
    }

    return 0;
}

29. Complete Calculator Program

#include <stdio.h>

int main()
{
    int a, b;
    char op;

    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &op);

    if (op == '+')
    {
        printf("Result = %d", a + b);
    }
    else if (op == '-')
    {
        printf("Result = %d", a - b);
    }
    else if (op == '*')
    {
        printf("Result = %d", a * b);
    }
    else if (op == '/')
    {
        if (b != 0)
        {
            printf("Result = %d", a / b);
        }
        else
        {
            printf("Cannot divide by zero");
        }
    }
    else
    {
        printf("Invalid operator");
    }

    return 0;
}

30. Practice Programs Using else-if

Practice the following programs:

  1. Create a grade calculator.
  2. Check whether a number is positive, negative, or zero.
  3. Classify a person by age group.
  4. Find the largest among three numbers.
  5. Find the smallest among three numbers.
  6. Create a simple calculator using operators.
  7. Check profit, loss, or no profit.
  8. Classify temperature into different ranges.
  9. Check marks and display a suitable grade.
  10. Create a menu-based program using an else-if ladder.

Always check the conditions in the correct order and remember that the first true condition is executed.

📌 Key Points

  • An else-if ladder is used to check multiple conditions.
  • Conditions are evaluated from top to bottom.
  • The first true condition is executed.
  • Once a condition is true, the remaining else-if conditions are skipped.
  • The final else is optional.
  • The final else executes when none of the conditions is true.
  • Condition order is important, especially for range-based conditions.
  • Use == for comparison and = for assignment.
  • Logical operators can be used inside else-if conditions.
  • Else-if ladders are useful for grades, ranges, menus, classifications, and multiple choices.

🧠 Quick Quiz

Question: In an else-if ladder, what happens after the first true condition is found?