C++ if else if else statement
The if-else if-else
statement in C++ is used for multi-way decision-making when there are several possible conditions to evaluate. It is an extension of the basic if
statement that allows you to check multiple conditions in sequence and execute the corresponding block of code when the first matching condition is found.
Syntax
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition1 is false and condition2 is true
} else if (condition3) {
// Code to execute if condition1 and condition2 are false and condition3 is true
} else {
// Code to execute if none of the above conditions are true
}
How It Works
- The
if-else if-else
structure starts by evaluatingcondition1
.- If
condition1
istrue
, the code block associated with it is executed, and the rest of the conditions are ignored. - If
condition1
isfalse
, the program checkscondition2
. Ifcondition2
istrue
, the corresponding code block runs. - This process continues until one of the conditions is found to be
true
or theelse
block is reached. - If none of the conditions are true, the
else
block is executed as a default.
- If
Example
Here is an example that demonstrates the use of if-else if-else
to determine a grade based on a score:
#include <iostream>
int main() {
int score;
std::cout << "Enter your score: ";
std::cin >> score;
if (score >= 90) {
std::cout << "Grade: A" << std::endl;
} else if (score >= 80) {
std::cout << "Grade: B" << std::endl;
} else if (score >= 70) {
std::cout << "Grade: C" << std::endl;
} else if (score >= 60) {
std::cout << "Grade: D" << std::endl;
} else {
std::cout << "Grade: F" << std::endl;
}
return 0;
}
Explanation
- In the above code:
- The program takes a score input from the user.
- It first checks if the score is
>= 90
. If it is, it prints "Grade: A". - If the score is not
>= 90
, it checks if it is>= 80
. If true, it prints "Grade: B". - This continues for each
else if
condition. - If the score is less than 60, the
else
block is executed, and "Grade: F" is printed.
Key Points
- Order Matters: The conditions are evaluated from top to bottom. The first condition that evaluates to
true
will execute its block of code, and the rest will be skipped. - Only One Block Executes: In an
if-else if-else
chain, only one block of code can execute, even if multiple conditions could potentially betrue
. The firsttrue
condition wins. - The
else
Block: Theelse
block is optional and acts as a default for when none of theif
orelse if
conditions aretrue
.
Example Without else
The else
block is not mandatory. If you do not include it, and none of the if
or else if
conditions are met, no action will be taken.
int number = 15;
if (number < 10) {
std::cout << "Number is less than 10" << std::endl;
} else if (number > 10 && number <= 20) {
std::cout << "Number is between 11 and 20" << std::endl; // Output: Number is between 11 and 20
}
// No `else` block here, so if the number doesn't match any condition, nothing happens.
Summary
if-else if-else
is useful for handling multiple possible conditions.- The conditions are evaluated in sequence, and only the block corresponding to the first
true
condition will be executed. - The
else
block is optional but can be used to handle cases where none of the preceding conditions are met.
The if-else if-else
statement makes decision-making in C++ more flexible and allows the program to respond to various scenarios effectively.