else if Statement in C++

 


The else if statement in C++ is used when multiple conditions need to be checked sequentially. It allows the program to execute different blocks of code based on different conditions.


1️⃣ Syntax of if-else if-else Statement

if (condition1) {
    // Code executes if condition1 is true
} else if (condition2) {
    // Code executes if condition1 is false but condition2 is true
} else if (condition3) {
    // Code executes if condition2 is also false but condition3 is true
} else {
    // Code executes if all conditions are false
}

2️⃣ Example: Checking a Student’s Grade

#include <iostream>
using namespace std;

int main() {
    int marks;
    cout << "Enter your marks: ";
    cin >> marks;

    if (marks >= 90) {
        cout << "Grade: A" << endl;
    } else if (marks >= 75) {
        cout << "Grade: B" << endl;
    } else if (marks >= 50) {
        cout << "Grade: C" << endl;
    } else {
        cout << "Grade: F (Fail)" << endl;
    }

    return 0;
}

Output:

Enter your marks: 85
Grade: B

📌 How it works?

  • If marks are 90 or above → Print "Grade: A".
  • If marks are between 75 and 89 → Print "Grade: B".
  • If marks are between 50 and 74 → Print "Grade: C".
  • If marks are below 50 → Print "Grade: F (Fail)".

3️⃣ Example: Checking Temperature Ranges

#include <iostream>
using namespace std;

int main() {
    int temp;
    cout << "Enter the temperature: ";
    cin >> temp;

    if (temp >= 30) {
        cout << "It's hot outside!" << endl;
    } else if (temp >= 20) {
        cout << "The weather is warm." << endl;
    } else if (temp >= 10) {
        cout << "It's cool outside." << endl;
    } else {
        cout << "It's cold outside!" << endl;
    }

    return 0;
}

Output:

Enter the temperature: 15
It's cool outside.

4️⃣ Example: Checking if a Number is Positive, Negative, or Zero

#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;

    if (num > 0) {
        cout << "The number is positive." << endl;
    } else if (num < 0) {
        cout << "The number is negative." << endl;
    } else {
        cout << "The number is zero." << endl;
    }

    return 0;
}

Output:

Enter a number: -5
The number is negative.

5️⃣ Nested if-else if Example

You can also use nested if-else if inside another if-else if structure.

#include <iostream>
using namespace std;

int main() {
    int age;
    cout << "Enter your age: ";
    cin >> age;

    if (age >= 18) {
        if (age >= 60) {
            cout << "You are a senior citizen." << endl;
        } else {
            cout << "You are an adult." << endl;
        }
    } else if (age > 12) {
        cout << "You are a teenager." << endl;
    } else {
        cout << "You are a child." << endl;
    }

    return 0;
}

Output:

Enter your age: 15
You are a teenager.

6️⃣ else if with Logical Operators (&&, ||)

Example: Checking Voting Eligibility

#include <iostream>
using namespace std;

int main() {
    int age;
    cout << "Enter your age: ";
    cin >> age;

    if (age >= 18 && age <= 60) {  
        cout << "You are eligible to work." << endl;
    } else if (age > 60) {
        cout << "You are retired." << endl;
    } else {
        cout << "You are too young to work." << endl;
    }

    return 0;
}

Output:

Enter your age: 65
You are retired.

7️⃣ else if vs. Multiple if Statements

Using Multiple if Statements (Incorrect)

int num = 10;

if (num > 0) {
    cout << "Positive number." << endl;
}
if (num % 2 == 0) {
    cout << "Even number." << endl;
}

Output:

Positive number.
Even number.

📌 Here, both if statements execute separately.
👉 If you want to check conditions in order, use else if.

Using else if (Correct)

int num = 10;

if (num > 0) {
    cout << "Positive number." << endl;
} else if (num % 2 == 0) {
    cout << "Even number." << endl;
}

📌 Now, only one condition executes! 🚀


Conclusion

  • else if is used when multiple conditions need to be checked.
  • If the first if condition is false, it checks the next else if condition.
  • If all conditions are false, the else block executes.
  • Use logical operators (&&, ||) to combine multiple conditions.
  • Avoid using multiple if statements when else if is needed.

Would you like an example of error handling (e.g., handling invalid input)? 🚀

Post a Comment

0 Comments