Logical Or Operator


Syntax

logical_or_expression:
       logical_and_expression
       logical_or_expression || logical_and_expression

Notes

The logical or groups left to right. For arithmetic operands, the returned result is of integer type and is 1 if either operand evaluates to 1 and is 0 when both operands evaluate to zero.

The or of two values is true only when either or both operands are true, as depicted in the truth table below:

P Q P||Q
0 0 0
0 1 1
1 0 1
1 1 1

The logical or operator applies the above truth table on a macroscopic level to its operands. That is, individual bits are not manipulated; rather, the operand is merely examined to see if it is zero or non-zero. Consider the examples below.

a = 1
b = 0
c = 0
e = a || b   // e is non-zero because a is non-zero.
f = b || c   // f is zero because both b and c are zero.
g = a || a   // g is non-zero because a is non-zero.

if a==2 || b                   // Note: a==2 evaluates to 0
 {you should not get here}     // b is 0
                               // 0 || 0 evaluates to 0
                               // Therefore the antecedent of the conditional is false.

The operator | is the bitwise or, and is not to be confused with the logical or operator. The logical or operator evaluates to 1 if either of its operands is non-zero and 0 otherwise (which is quite different to bitwise or).