if, else if, and else Statements in C++
In the C++ programming language, the if, else if, and else statements are used to resolve program conditional flow statements based on single to multiple if, else if, and else branch conditional flow statements in a program. The if, else if, and else statements help C++ users control the flow of current program execution based on whether a particular user-defined custom condition is true or false.

So, let’s explore the if, else if, and else control flow statements in C++ programming.
If Statement in C++ Programming.
The if statement in C++ is used to execute a particular custom block of user-defined source code when a user-defined custom if condition in a program is always true. If the user-defined if statement condition is false, the program created inside the current if condition statement skips the source code block.
Syntax of an If Statement in C++.
if (custom condition) {
// Here we create a program to execute code when the if condition is true.
}
Example of an If Statement in C++.
#include <iostream>
using namespace std;
int main() {
int p = 2;
if (p > 1) {
cout << “p value is greater than 2” << endl;
}
return 0;
}
Explanation of an If Statement in C++.
- In this example, the value of the variable p is defined as 2. Therefore, the if condition p > 1 is true. Therefore, the message “p value is greater than 2” will be printed on the console screen.
else Statement in C++ Programming.
The else statement in C++ programs is used to indicate or resolve a particular block of user-defined program source code. The else statement is executed when a user-defined if condition in a program returns false. This means the else statement is run or executed only when the if condition is skipped.
else Statement Syntax in C++.
if (condition) {
// here we write if program source code to run when the if condition is true
} else {
// here else statement code to run only when the if condition is false
}
Example of an else statement in C++.
#include <iostream>
using namespace std;
int main() {
int value = 1;
if (value > 2) {
cout << “value is greater than 2” << endl;
} else {
cout << “value is smaller than or equal to 2” << endl;
}
return 0;
}
Explanation of an else statement.
- In this example, since the value variable is defined to have a value of 1, the value > 2 if expression condition statement returns false. Consequently, the message “value is smaller than or equal to 2” is printed on the console screen.
else if Statement in C++ Programming.
The else if statement in a C++ program provides C++ users with the ability to analyze or test multiple user-defined condition expressions simultaneously. The else if statement is used when the C++ user has two or more condition expressions or statement options to evaluate or test. If the first user-defined condition fails, the else if statement is used. It then checks or analyzes the next else if condition, and so on for the remaining else if expressions. If none of the conditions in the entire program is true, the program will execute the code in the user-defined else code block (if defined) at the end.
else if Statement Syntax in C++.
if (testcondition1) {
// here we write source code to execute when only if testcondition1 is true
} else if (testcondition2) {
// here we write source code to execute when only if testcondition2 is true
} else {
// here we write source code to execute if neither testcondition1 nor testcondition2 is true
}
Example of an else if statement.
#include <iostream>
using namespace std;
int main() {
int age = 19;
if (age > 21) {
cout << “age is greater than 21” << endl;
} else if (age > 11) {
cout << “age is greater than 11 but age is less than or equal to 21” << endl;
} else {
cout << “age is less than or equal to 11” << endl;
}
return 0;
}
Explanation of an else if statement.
- In this example, the if condition age > 21 is defined as false. This moves to the next condition.
- After this, the else if condition age > 11 is defined as true. Therefore, the message “age is greater than 11 but age is less than or equal to 21” will be printed on the console screen.
Multiple else if statements are chained in C++ programming.
C++ users can use multiple else if block statements to check or analyze multiple user-defined if, else if, and else expression conditions one after another in a program.
Example of multiple else if statements.
#include <iostream>
using namespace std;
int main() {
int age = 21;
if (age > 51) {
cout << “age is greater than 51” << endl;
} else if (age > 31) {
cout << “age is greater than 31 but less than or equal to 51” << endl;
} else if (age > 11) {
cout << “age is greater than 11 but less than or equal to 31” << endl;
} else {
cout << “age is less than or equal to 11” << endl;
}
return 0;
}
Explanation of a chained multiple else if statements.
- In this example, age is defined as 21. Therefore, the user-defined condition age > 11 is true. This results in the statement “age is greater than 11 but less than or equal to 31” being printed on the console screen.
Nested if, else if, and else statements in C++ programming.
C++ users can define multiple nested block conditions by nesting if, else if, and else statements within each other to create more complex logical conditions or expressions in a program.
Example of nested if, else if, and else statements.
#include <iostream>
using namespace std;
int main() {
int p = 11, q = 5;
if (p > q) {
if (p > 8) {
cout << “p value is greater than q value and greater than 8” << endl;
} else {
cout << “p value is greater than q value but not greater than 8” << endl;
}
} else {
cout << “p value is not greater than q value” << endl;
}
return 0;
}
Explanation of nested if, else if, and else statements.
- In this example, the outer if statement checks whether p > q, which is true (11 > 5). This causes it to enter the block. The nested if statement inside it checks whether p > 8, which is also true. Therefore, the message “p value is greater than q value and greater than 8” will be printed on the console screen.
Using logical operators with if, else if, and else statements in C++ programming.
C++ users can use logical operators (such as AND, &&, OR ||, NOT!) to combine or group multiple branch condition expressions in if, else if, or else statements in a program.
Example of operators with if, else if, and else statements.
#include <iostream>
using namespace std;
int main() {
int p = 7, q = 3;
if (p > 4 && q < 7) {
cout << “here both p and q logical conditions are true” << endl;
} else {
cout << “here one or both and logical operator conditions are false” << endl;
}
return 0;
}
Explanation of operators with if, else if, and else statements.
- In this example, both conditions p > 4 && q < 7 are defined as true. This results in the statement “here both p and q are true” being printed on the console.
Example of the || OR operator.
#include <iostream>
using namespace std;
int main() {
int p = 7, q = 17;
if (p > 3 || q > 9) {
cout << “here p || q is the minimum one logical operator condition” << endl;
} else {
cout << “no above || logical operator condition is true” << endl;
}
return 0;
}
Explanation of the || OR logical operator.
- In this example, the condition q > 9 is true (17 > 9). Therefore, the else logical operator will print the statement “where p || Q logical operator minimum one condition is true”.
Detail Explanation of if, else if, and else statement in c++
| Keyword | if, else if, and else Statements Description | Each Example |
| If statement | If statement used to Tests a condition and executes a block of code if the condition is actually true. | if (age > 21) { cout << “your age is mature”; } |
| Else statement | Else statement Executes only a block of program code if the if previous user define condition in false state. | else { cout << ” your age is not mature”; } |
| else if statement | Else if statement used t Tests another multiple program condition if the previous all if condition is false or none to execute. | else if (value == 9) { cout << “value is equal to 9”; } |
| Logical operators | We can use logical operators (&&, `||, !) in if, else if, and else statement | if (p > 4 && q < 7), if (p > 3 || q > 9) |
if, else if, and else statements summary in C++.
- When a C++ user executes a custom if block of source code in a program, and any of the conditions defined in it is true, it prints the corresponding statement to the console screen.
- The else if statement in a C++ program allows multiple additional nested if block conditions to be checked or analysed when a previous if or else if statement condition fails.
- The else statement in a C++ program executes a code block of else statements within the current statement if none of the previous user-defined conditions is true.

