Or operator : ||
And operator : &&
Please thank, like, follow and improve
logical operators are used to test multiple conditions i.e. combine two or more conditions
They are:
and - &
or - |
not - !
and operators return true if and only if both operators are true.
or operators return true if at least one operator is true.
not operator inverts a situation or condition: if true it turns it false and vice-versa.
Logical Operators:
They are used to combine two or more conditions/constraints or to complement
the evaluation of the original condition under consideration. They are
described below:
Logical AND operator: The ‘&&’ operator returns true when both the conditions
under consideration are satisfied. Otherwise it returns false. For example, a
&& b returns true when both a and b are true (i.e. non-zero).
Logical OR operator: The ‘||’ operator returns true even if one (or both) of
the conditions under consideration is satisfied. Otherwise it returns false.
For example, a || b returns true if one of a or b or both are true
(i.e. non-zero). Of course, it returns true when both a and b are true.
Logical NOT operator: The ‘!’ operator returns true the condition in
consideration is not satisfied. Otherwise it returns false. For example,
!a returns true if a is false, i.e. when a=0.
#include <iostream>
using namespace std;
int main() {
bool result;
result = (3 != 5) && (3 < 5); // true
cout << "(3 != 5) && (3 < 5) is " << result << endl;
result = (3 == 5) && (3 < 5); // false
cout << "(3 == 5) && (3 < 5) is " << result << endl;
result = (3 == 5) && (3 > 5); // false
cout << "(3 == 5) && (3 > 5) is " << result << endl;
result = (3 != 5) || (3 < 5); // true
cout << "(3 != 5) || (3 < 5) is " << result << endl;
result = (3 != 5) || (3 > 5); // true
cout << "(3 != 5) || (3 > 5) is " << result << endl;
result = (3 == 5) || (3 > 5); // false
cout << "(3 == 5) || (3 > 5) is " << result << endl;
result = !(5 == 2); // true
cout << "!(5 == 2) is " << result << endl;
result = !(5 == 5); // false
cout << "!(5 == 5) is " << result << endl;
return 0;
}