C++ Logical Operator
Logical Operators ( !, &&, || )
Logical operators are used to determine the logic between variables or values:
Operator | Name | Description | Example |
---|---|---|---|
&& | Logical AND | Returns true if both statements are true | x < 5 && x < 10 |
|| | Logical OR | Returns true if one of the statements is true | x < 5 || x < 4 |
! | Logical NOT | Reverse the result, returns false if the result is true | !(x < 5 && x < 10) |
Example 1
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x > 3 && x < 10); // returns true (1) because 5 is greater than 3 AND 5 is less than 10
return 0;
}
Example 2
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x > 3 || x < 4); // returns true (1) because one of the conditions are true (5 is greater than 3, but 5 is not less than 4)
return 0;
}
Example 3
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (!(x > 3 && x < 10)); // returns false (0) because ! (not) is used to reverse the result
return 0;
}
The operator ! is the C++ operator for the Boolean operation NOT. It has only one operand, to its right, and inverts it, producing false if its operand is true, and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. For example:
!(6 <= 4) // evaluates to true because (6 <= 4) would be false
!true // evaluates to false
!false // evaluates to true
The logical operators && and || are used when evaluating two expressions to obtain a single relational result. The operator && corresponds to the Boolean logical operation AND, which yields true if both its operands are true, and false otherwise. The following panel shows the result of operator && evaluating the expression a&&b:
The operator || corresponds to the Boolean logical operation OR, which yields true if either of its operands is true, thus being false only when both operands are false. Here are the possible results of a||b:
For example:
( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false )
When using the logical operators, C++ only evaluates what is necessary from left to right to come up with the combined relational result, ignoring the rest. Therefore, in the last example ((5==5)||(3>6)), C++ evaluates first whether 5==5 is true, and if so, it never checks whether 3>6 is true or not. This is known as short-circuit evaluation, and works like this for these operators:
operator | short-circuit |
---|---|
&& | if the left-hand side expression is false, the combined result is false (the right-hand side expression is never evaluated). |
|| | if the left-hand side expression is true, the combined result is true (the right-hand side expression is never evaluated). |
This is mostly important when the right-hand expression has side effects, such as altering values:
Here, the combined conditional expression would increase i by one, but only if the condition on the left of && is true, because otherwise, the condition on the right-hand side (++i<n ) is never evaluated.
Comments
Post a Comment