CPP Tutorial



C++ ELSE IF


Else If Statement in C++

The else if statement allows you to check multiple conditions in sequence. It helps your program choose among more than two possible paths.

📐 Syntax

if (condition1) {
    // Executes if condition1 is true
} else if (condition2) {
    // Executes if condition1 is false and condition2 is true
} else {
    // Executes if none of the above conditions are true
}
  

🔎 How It Works

  • The program checks condition1. If it is true, runs the first block.
  • If condition1 is false, it checks condition2. If true, runs the second block.
  • If none of the conditions are true, the else block runs (if present).

💡 Example

#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" << endl;
    }

    return 0;
}
  

⚙️ Explanation

- The program asks for your marks.
- It checks the highest grade condition first (marks >= 90), then goes down.
- Prints the grade based on the first true condition.
- If no conditions match, it prints Grade: F.

Summary

  • Use else if to test multiple conditions sequentially.
  • Only one block of code will run — the first condition that is true.
  • else is optional, for a default action.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review