The if and if-else conditionals are fundamental control structures in C++ that allow making decisions based on boolean evaluations (true or false).
If you want to learn more, check out the Introduction to Programming Course
The IF Conditional
The if structure evaluates a boolean expression and executes a block of code only if the expression evaluates to true. The basic syntax of an if conditional in C++ is:
if (condition) {
// Code to execute if the condition is true
}
Let’s see it with an example:
int number = 10;
if (number > 5) {
std::cout << "The number is greater than 5" << std::endl;
}
In this example, the condition number > 5 evaluates to true, so the message “The number is greater than 5” is printed to the console.
The IF ELSE Conditional
The if conditional allows you to add an alternative else block of code that will be executed if the if condition is false. The basic syntax is:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Let’s see it with an example:
int number = 3;
if (number > 5) {
std::cout << "The number is greater than 5" << std::endl;
} else {
std::cout << "The number is not greater than 5" << std::endl;
}
In this case, the condition number > 5 is false, so the code block inside the else is executed, printing “The number is not greater than 5” to the console.
The IF ELSE-IF Conditional
To evaluate multiple conditions, you can chain multiple if / else-if / else blocks. This allows evaluating several conditions in sequence until one of them is true.
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition1 is false and condition2 is true
} else {
// Code to execute if all previous conditions are false
}
Practical Examples
Determine if a number is less than 5 or greater than 15
In this example, we determine if a number is less than 5, greater than 15, or in the intermediate range.
int number = 10;
if (number > 15) {
std::cout << "The number is greater than 15" << std::endl;
} else if (number > 5) {
std::cout << "The number is greater than 5 but less than or equal to 15" << std::endl;
} else {
std::cout << "The number is 5 or less" << std::endl;
}
Here, we evaluate the conditions sequentially: number > 15 is false, but number > 5 is true, so “The number is greater than 5 but less than or equal to 15” is printed.
Determine the Largest of Three Numbers
In this example, we find the largest of three numbers using if-else structures.
int a = 5, b = 10, c = 3;
int largest;
if (a > b && a > c) {
largest = a;
} else if (b > a && b > c) {
largest = b;
} else {
largest = c;
}
std::cout << "The largest number is: " << largest << std::endl;
Verify Grade Ranges
In this example, we assign a letter grade based on a numerical score using a series of if-else conditions.
int grade = 85;
std::string range;
if (grade >= 90) {
range = "A";
} else if (grade >= 80) {
range = "B";
} else if (grade >= 70) {
range = "C";
} else if (grade >= 60) {
range = "D";
} else {
range = "F";
}
std::cout << "The grade is: " << range << std::endl;
