The && (logical AND) operator indicates whether both operands are true.
If both operands have nonzero values, 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 both operands have values 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 AND) operator, the && operator guarantees left-to-right evaluation of the operands. If the left operand evaluates to 0 (or false), the right operand is not evaluated.
The following examples show how the expressions that contain the logical
AND operator are evaluated:
Expression | Result |
---|---|
1 && 0 | false or 0 |
1 && 4 | true or 1 |
0 && 0 | false or 0 |
The following example uses the logical AND operator to avoid division by zero:
(y != 0) && (x / y)
The expression x / y is not evaluated when y != 0 evaluates to 0 (or false).
Related References