Logical Operators
Purpose:
- Used to form more complex conditions by combining simple conditions.
Mechanics:
| Operator | Example | Description |
| && | A && B | logical AND |
| || | A || B | logical OR |
| ! | !A | logical NOT (logical negation) |
| & | true & true | boolean logical AND (both operands must be booleans) |
| ^ | false ^ false | boolean logical exclusive OR (both operands must be booleans) |
| | | true | false | boolean logical inclusive OR (both operands must be booleans) |
Truth Tables:
| A | B | A && B |
| T | T | T |
| T | F | F |
| F | T | F |
| F | F | F |
| A | B | A || B |
| T | T | T |
| T | F | T |
| F | T | T |
| F | F | F |
| A | !A |
| T | F |
| F | T |
Boolean Logical Operators:
both operands must be booleans| & | the result value is true if both operand values
are true; otherwise, the result is
false |
| ^ | exclusive OR, the result value is true if the
operand values are different; otherwise, the result is
false |
| | | inclusive OR, the result value is false if both
operand values are false; otherwise the result is
true |
Restrictions:
- To use the boolean logical operators, both operands must be
booleans.
&&and||are more commonly used.
email suggestions to: cs015tas@cs.brown.edu
