Now we know how to input numbers and are familiar with several arithmetic operations. Today we will look at something different: conditional operators.
Conditional operators allow you to perform different actions depending on conditions. They are used when you need to execute code only in certain situations.
if OperatorThe if operator is used to execute code if a specified condition is true. Here is its general syntax:
For example, if we have a variable a, and we want to check if it is greater than 100:
If the value of variable a is greater than 100, then "Greater than 100" will be displayed.
else if OperatorThe else if operator allows you to execute code if the previous condition is false, but the current condition is true. This is useful when you have multiple conditions to check.
Example of using else if:
If the value of variable a is less than 100, "Less than 100" will be displayed.
The else if operator can only be used as a subsequent check after the if operator, meaning in the hierarchy of checks there should always be an if first followed by several else ifs.
else OperatorThe else operator is executed if all previous conditions are false. It allows you to handle all other cases and does not require checking any additional condition.
Example of using else:
If the value of variable a is neither greater nor less than 100, "Equal to 100" will be displayed.
The else operator can be used either after the if operator or after the else if operator, but in the hierarchy it should always be last.
and and orNow let's look at two other operations called logical operators and and or. These operators can be used inside our conditional if operator.
and Operator - &&The and operator allows you to check if both conditions are met simultaneously. Here's an example:
In this example, we check if the value of variable a is even (a % 2 == 0) and if it is greater than 100 (a > 100). If both conditions are true, then the code inside if is executed. The and operator simplifies performing such checks where both conditions need to be met.
or Operator - ||The or operator is used when you need at least one of the conditions to be met. Here's an example:
In this example, we check if the value of variable a is even (a % 2 == 0) or if it is greater than 100 (a > 100). If at least one of the conditions is true, then the code inside if is executed. The or operator allows you to consider different scenarios where at least one condition needs to be met.
and and or OperatorsLet's look at the following example:
Here we first use the and operator to check if a is even and greater than 100. If this is satisfied, we output "Even and greater than 100". Then we use the or operator to check if a is even or greater than 100. If this is satisfied, we output "Even or greater than 100".
The and and or operators help you create more complex conditions, considering various scenarios in your programs.
This program analyzes the entered number and determines whether it is a positive even, positive odd, negative number, or zero.
if (condition) {
// Code executed if condition is true
}
int a;
cin >> a;
if (a > 100) {
cout << "Greater than 100" << endl;
}
if (condition1) {
// Code executed if condition1 is true
} else if (condition2) {
// Code executed if condition1 is false, but condition2 is true
}
int a;
cin >> a;
if (a > 100) {
cout << "Greater than 100" << endl;
} else if (a < 100) {
cout << "Less than 100" << endl;
}
if (condition1) {
// Code executed if condition1 is true
} else if (condition2) {
// Code executed if condition1 is false, but condition2 is true
} else {
// Code executed if none of the conditions are true
}
int a;
cin >> a;
if (a > 100) {
cout << "Greater than 100" << endl;
} else if (a < 100) {
cout << "Less than 100" << endl;
} else {
cout << "Equal to 100" << endl;
}
if (a % 2 == 0 && a > 100) {
cout << "Even and greater than 100" << endl;
}
if (a % 2 == 0 || a > 100) {
cout << "Even or greater than 100" << endl;
}
if (a % 2 == 0 && a > 100) {
cout << "Even and greater than 100" << endl;
} else if (a % 2 == 0 || a > 100) {
cout << "Even or greater than 100" << endl;
}
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number > 0 && number % 2 == 0) {
cout << "Positive even number" << endl;
} else if (number > 0 && number % 2 == 1) {
cout << "Positive odd number" << endl;
} else if (number < 0) {
cout << "Negative number" << endl;
} else {
cout << "Zero" << endl;
}
return 0;
}