Logical Operators
Logical operators allow performing operations on boolean values, true and false. In C++0 is equivalent
to the boolean value false and any value different from 0 is equivalent to the boolean value true.
true and false are actual C++ keywords and can be used
interchangeably with the numerical values equivalent to them.
The logical operators are:
&& AND: returns true if both operands are true || OR: returns true if any of the two operands is true ! NOT: returns the opposite of the operand it precedes Often boolean operatorns are applied to the results of relational operators:
4 < 5 && 7 < 9 returns 1 (true)5 >= 8 || 3 != 3 returns 0 (false)
Example
#include <iostream>
using namespace std;
int main()
{
cout << "true && false " << (true && false) << endl;
cout << "false && false " << (false && false)<< endl;
cout << "true || false " << (true || false) << endl;
cout << "false || false " << (false || false) << endl;
cout << "!1 " << !1 << endl;
cout << "!false " << !false << endl;
}
Output
true && false 0 false && false 0 true || false 1 false || false 0 !1 0 !false 1