The else if statement allows you to check multiple conditions in sequence. It helps your program choose among more than two possible paths.
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 }
condition1>. If it is true, runs the first block.
condition1
is false, it checks condition2
. If true, runs the second block.else
block runs (if present).#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; }
- 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.
else if
to test multiple conditions sequentially.else
is optional, for a default action.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!