Logical (Boolean) Operators
Logical expressions, like comparison expressions, return a true (1) or false (0) value when processed. Logical operators combine two comparisons and return the true (1) or false (0) value depending on the results of the comparisons.
The logical operators are:
- Operator
- Meaning
- &
- AND Returns 1 if both comparisons are true. For example:
(4 > 2) & (a = a) /* true, so result is 1 */ (2 > 4) & (a = a) /* false, so result is 0 */
- |
- Inclusive OR Returns 1 if at least one comparison is true. For example:
(4 > 2) | (5 = 3) /* at least one is true, so result is 1 */ (2 > 4) | (5 = 3) /* neither one is true, so result is 0 */
- &&
- Exclusive OR Returns 1 if only one comparison (but not both) is true. For example:
(4 > 2) && (5 = 3) /* only one is true, so result is 1 */ (4 > 2) && (5 = 5) /* both are true, so result is 0 */ (2 > 4) && (5 = 3) /* neither one is true, so result is 0 */
- Prefix \
- Logical NOT Returns the opposite response. For example:
\ 0 /* opposite of 0, so result is 1 */ \ (4 > 2) /* opposite of true, so result is 0 */