Logical OR operator ||

The || (logical OR) operator indicates whether either operand is true.

C only 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.

C++ 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.

Note:
The logical OR (||) should not be confused with the bitwise OR (|) operator. For example:
   1 || 4 evaluates to 1 (or    true)
while
   1 | 4 evaluates to 5


[ Top of Page | Previous Page | Next Page | Contents | Index ]