Logical operators are special symbols that allow us to combine conditions and perform operations based on the result of those conditions. The most common logical operators in C++ are:
&&
(AND): Returns true if both conditions are true.||
(OR): Returns true if at least one of the conditions is true.!
(NOT): Inverts the value of the condition.
More information about logical operators read more
List of logical operators
Logical AND (&&
)
The logical AND operator allows us to evaluate two conditions and returns true
if both conditions are true, or false
otherwise. For example:
bool condition1 = true;
bool condition2 = false;
bool result = condition1 && condition2; // result will be equal to false
Logical OR (||
)
The logical OR operator allows us to evaluate two conditions and returns true
if at least one of the conditions is true, or false
otherwise. For example:
bool condition1 = true;
bool condition2 = false;
bool result = condition1 || condition2; // result will be equal to true
Logical NOT (!
)
The logical NOT operator allows us to invert the value of a condition. If the condition is true
, it returns false
, and if the condition is false
, it returns true
. For example:
bool condition = true;
bool result = !condition; // result will be equal to false
Usage Examples
Combination with &&
The &&
(logical AND) operator is used to combine two boolean expressions. The entire expression is evaluated as true
only if both subexpressions are true
.
int age = 25;
bool hasLicense = true;
bool canDrive = (age >= 18) && hasLicense; // true
Combination with ||
The ||
(logical OR) operator is used to combine two boolean expressions. The entire expression is evaluated as true
if at least one of the subexpressions is true
.
bool isRaining = false;
bool isCold = true;
bool wantsToStayHome = isRaining || isCold; // true
Negation with !
The !
(logical NOT) operator is used to negate a boolean expression. The entire expression is evaluated as true
if the subexpression is false
, and vice versa.
bool isDay = true;
bool isNight = !isDay; // false
Combining Logical Operators
It is possible to combine multiple logical operators in a single expression to create more complex conditions.
For example:
int number = 15;
bool isMultipleOf3 = (number % 3 == 0);
bool isMultipleOf5 = (number % 5 == 0);
bool isMultipleOf3_or_5 = isMultipleOf3 || isMultipleOf5; // true, since 15 is a multiple of 3 or 5
bool isMultipleOf3_and_5 = isMultipleOf3 && isMultipleOf5; // true, since 15 is a multiple of 3 and 5 simultaneously
bool isNotMultipleOf3 = !isMultipleOf3; // false
Precedence of Logical Operators
When combining multiple logical operators in an expression, it is important to understand their precedence in order to correctly evaluate the expression.
The precedence of logical operators in C++ follows this order:
!
(NOT)&&
(AND)||
(OR)
Therefore, in an expression with &&
and ||
, &&
is evaluated before ||
. However, it is not advisable to abuse this when writing our code. Feel free to use parentheses if it improves readability.
For example:
int a = 5;
int b = 10;
int c = 15;
bool result = (a > b) || (b < c) && (a == c); // true
// Evaluated as: (a > b) || ((b < c) && (a == c))
To improve clarity, parentheses can be used:
bool result = (a > b) || ((b < c) && (a == c)); // true