The || (logical OR) operator indicates whether either operand is true.
If
either of the operands has a nonzero value, the result has the value 1. Otherwise, the result has the value 0. The type of the
result is int. Both operands must have a arithmetic or pointer
type. The usual arithmetic conversions on each operand are performed.
If either operand has a value of true, the result
has the value true. Otherwise, the result has the value false. Both operands are implicitly converted to bool and the
result type is bool.
Unlike the | (bitwise inclusive OR) operator, the || operator guarantees left-to-right evaluation of the operands. If the left operand has a nonzero (or true) value, the right operand is not evaluated.
The following examples show how expressions that contain the logical OR operator are evaluated:
Expression | Result |
---|---|
1 || 0 | true or 1 |
1 || 4 | true or 1 |
0 || 0 | false or 0 |
The following example uses the logical OR operator to conditionally increment y:
++x || ++y;
The expression ++y is not evaluated when the expression ++x evaluates to a nonzero (or true) quantity.