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 one after another. The program checks the first condition. If it is false, it checks the next condition, and so on. When a true condition is found, its block is executed and the remaining conditions are skipped.

Note: Use an else-if ladder when there are multiple possible conditions or outcomes, such as grades, marks, age groups, or menu choices.

1. What is an else-if Ladder?

An else-if ladder allows us to test multiple conditions in sequence.

if (condition1) {
    // code
}
else if (condition2) {
    // code
}
else if (condition3) {
    // code
}
else {
    // code
}

2. Why Use else-if?

A simple if-else statement provides two choices. When we have more than two possible choices, an else-if ladder is useful.

For example:

  • Marks 90 or above → Grade A
  • Marks 75 or above → Grade B
  • Marks 60 or above → Grade C
  • Marks 40 or above → Grade D
  • Below 40 → Fail

3. Basic else-if Example

int marks = 75;

if (marks >= 90) {
    std::cout << "Grade A";
}
else if (marks >= 75) {
    std::cout << "Grade B";
}
else {
    std::cout << "Below Grade B";
}

The first condition is false, but the second condition is true, so Grade B is displayed.

4. Syntax of else-if Ladder

if (condition1) {

    // statements

}
else if (condition2) {

    // statements

}
else if (condition3) {

    // statements

}
else {

    // default statements

}

There can be multiple else-if blocks between the initial if and the final else.

5. Checking Grades

int marks = 85;

if (marks >= 90) {
    std::cout << "Grade A";
}
else if (marks >= 80) {
    std::cout << "Grade B";
}
else if (marks >= 70) {
    std::cout << "Grade C";
}
else {
    std::cout << "Needs improvement";
}

6. Checking Multiple Age Groups

int age = 25;

if (age < 13) {
    std::cout << "Child";
}
else if (age < 20) {
    std::cout << "Teenager";
}
else if (age < 60) {
    std::cout << "Adult";
}
else {
    std::cout << "Senior";
}

7. Checking Positive, Negative, or Zero

An else-if ladder can handle three possible results.

int number = 0;

if (number > 0) {
    std::cout << "Positive";
}
else if (number < 0) {
    std::cout << "Negative";
}
else {
    std::cout << "Zero";
}

8. Taking Marks from User

#include <iostream>

int main() {

    int marks;

    std::cout << "Enter marks: ";
    std::cin >> marks;

    if (marks >= 90) {
        std::cout << "Grade A";
    }
    else if (marks >= 75) {
        std::cout << "Grade B";
    }
    else if (marks >= 60) {
        std::cout << "Grade C";
    }
    else if (marks >= 40) {
        std::cout << "Grade D";
    }
    else {
        std::cout << "Fail";
    }

    return 0;
}

9. Order of Conditions

The order of conditions is very important in an else-if ladder. Conditions are checked from top to bottom.

int marks = 95;

if (marks >= 90) {
    std::cout << "A";
}
else if (marks >= 80) {
    std::cout << "B";
}

The first condition is true, so the second condition is not checked.

10. Only One Block Executes

int marks = 85;

if (marks >= 90) {
    std::cout << "A";
}
else if (marks >= 80) {
    std::cout << "B";
}
else if (marks >= 70) {
    std::cout << "C";
}

Although 85 is also greater than 70, only B is displayed because the first true condition ends the ladder.

11. else-if without Final else

The final else is optional.

int age = 20;

if (age < 13) {
    std::cout << "Child";
}
else if (age < 20) {
    std::cout << "Teenager";
}

If no condition is true, nothing is executed because there is no final else.

12. Using Relational Operators

Relational operators are commonly used in else-if conditions.

int number = 50;

if (number > 50) {
    std::cout << "Greater than 50";
}
else if (number == 50) {
    std::cout << "Equal to 50";
}
else {
    std::cout << "Less than 50";
}

13. Using Logical AND

Multiple conditions can be combined using the && operator.

int age = 25;

if (age < 13) {
    std::cout << "Child";
}
else if (age >= 13 && age < 20) {
    std::cout << "Teenager";
}
else {
    std::cout << "Adult";
}

14. Checking Temperature

int temperature = 30;

if (temperature < 10) {
    std::cout << "Cold";
}
else if (temperature < 25) {
    std::cout << "Cool";
}
else if (temperature < 35) {
    std::cout << "Warm";
}
else {
    std::cout << "Hot";
}

15. Checking Salary Range

int salary = 30000;

if (salary < 20000) {
    std::cout << "Low salary";
}
else if (salary < 50000) {
    std::cout << "Medium salary";
}
else {
    std::cout << "High salary";
}

16. Checking Divisibility

int number = 15;

if (number % 3 == 0 && number % 5 == 0) {
    std::cout << "Divisible by both 3 and 5";
}
else if (number % 3 == 0) {
    std::cout << "Divisible by 3";
}
else if (number % 5 == 0) {
    std::cout << "Divisible by 5";
}
else {
    std::cout << "Not divisible by 3 or 5";
}

17. Checking Day Number

int day = 3;

if (day == 1) {
    std::cout << "Monday";
}
else if (day == 2) {
    std::cout << "Tuesday";
}
else if (day == 3) {
    std::cout << "Wednesday";
}
else {
    std::cout << "Other day";
}

18. Checking Month Number

int month = 2;

if (month == 1) {
    std::cout << "January";
}
else if (month == 2) {
    std::cout << "February";
}
else if (month == 3) {
    std::cout << "March";
}
else {
    std::cout << "Another month";
}

19. Checking Character

char grade = 'B';

if (grade == 'A') {
    std::cout << "Excellent";
}
else if (grade == 'B') {
    std::cout << "Very Good";
}
else if (grade == 'C') {
    std::cout << "Good";
}
else {
    std::cout << "Needs improvement";
}

20. Checking Login Role

#include <iostream>
#include <string>

int main() {

    std::string role = "teacher";

    if (role == "admin") {
        std::cout << "Admin Dashboard";
    }
    else if (role == "teacher") {
        std::cout << "Teacher Dashboard";
    }
    else if (role == "student") {
        std::cout << "Student Dashboard";
    }
    else {
        std::cout << "Unknown role";
    }

    return 0;
}

21. Practical Electricity Bill Example

int units = 250;

if (units <= 100) {
    std::cout << "Low usage";
}
else if (units <= 200) {
    std::cout << "Medium usage";
}
else if (units <= 300) {
    std::cout << "High usage";
}
else {
    std::cout << "Very high usage";
}

22. Practical Shopping Discount Example

double amount = 6000;

if (amount >= 10000) {
    std::cout << "20% discount";
}
else if (amount >= 5000) {
    std::cout << "10% discount";
}
else if (amount >= 2000) {
    std::cout << "5% discount";
}
else {
    std::cout << "No discount";
}

23. Taking User Input for Grade

#include <iostream>

int main() {

    int marks;

    std::cout << "Enter your marks: ";
    std::cin >> marks;

    if (marks >= 90) {
        std::cout << "Grade A";
    }
    else if (marks >= 75) {
        std::cout << "Grade B";
    }
    else if (marks >= 60) {
        std::cout << "Grade C";
    }
    else if (marks >= 40) {
        std::cout << "Grade D";
    }
    else {
        std::cout << "Fail";
    }

    return 0;
}

24. Using Decimal Values

An else-if ladder can also work with floating-point values.

double percentage = 82.5;

if (percentage >= 90.0) {
    std::cout << "Excellent";
}
else if (percentage >= 75.0) {
    std::cout << "Very Good";
}
else if (percentage >= 60.0) {
    std::cout << "Good";
}
else {
    std::cout << "Needs improvement";
}

25. else-if with Nested Conditions

An else-if ladder can also contain another if statement inside one of its blocks.

int marks = 85;

if (marks >= 90) {

    std::cout << "Grade A";

}
else if (marks >= 75) {

    std::cout << "Grade B";

    if (marks >= 85) {
        std::cout << " - Very good score";
    }

}
else {

    std::cout << "Below Grade B";

}

26. Common Mistake: Wrong Order

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

int marks = 95;

if (marks >= 40) {
    std::cout << "Pass";
}
else if (marks >= 90) {
    std::cout << "Grade A";
}

The first condition is already true for 95, so the second condition will never be reached. More specific conditions should generally be checked first when using ranges like this.

27. Complete Student Grade Program

#include <iostream>

int main() {

    int marks;

    std::cout << "Enter marks: ";
    std::cin >> marks;

    if (marks > 100 || marks < 0) {

        std::cout << "Invalid marks";

    }
    else if (marks >= 90) {

        std::cout << "Grade A";

    }
    else if (marks >= 75) {

        std::cout << "Grade B";

    }
    else if (marks >= 60) {

        std::cout << "Grade C";

    }
    else if (marks >= 40) {

        std::cout << "Grade D";

    }
    else {

        std::cout << "Fail";

    }

    return 0;
}

This example first checks for invalid marks and then checks the grades from the highest range to the lowest.

28. else-if vs Multiple Separate if Statements

An else-if ladder stops after finding the first true condition. Separate if statements can check every condition independently.

else-if ladder:

if (marks >= 90) {
    std::cout << "A";
}
else if (marks >= 80) {
    std::cout << "B";
}

For mutually exclusive choices, an else-if ladder is useful because only the first matching block is executed.

29. Common else-if Mistakes

  • Using else if without an initial if.
  • Writing conditions in an unsuitable order.
  • Forgetting the final else when a default case is required.
  • Using = instead of == for comparisons.
  • Using incorrect logical operators.
  • Forgetting braces when multiple statements are used.
  • Assuming every true condition will execute. Only the first true condition in the ladder executes.
if (condition1) {
    // first choice
}
else if (condition2) {
    // second choice
}
else {
    // default choice
}

30. else-if Ladder – Final Summary

Part Purpose
if Checks the first condition.
else if Checks another condition when previous conditions are false.
else Runs when none of the previous conditions are true.
Order Conditions are checked from top to bottom.
Execution Only the first true block is executed.
if (condition1) {
    // statement
}
else if (condition2) {
    // statement
}
else if (condition3) {
    // statement
}
else {
    // default statement
}

📌 Key Points

  • An else-if ladder is used to check multiple conditions.
  • Conditions are evaluated from top to bottom.
  • The first true condition executes its block.
  • After one block executes, the remaining else-if conditions are skipped.
  • The final else is optional.
  • The order of conditions is important.
  • Relational and logical operators can be used in conditions.
  • Else-if ladders are useful for grades, ranges, categories, and choices.
  • Always check more specific range conditions before broader conditions when appropriate.
  • Use clear and readable conditions to make programs easier to understand.

🧠 Quick Quiz

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