Logical AND operator &&
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 an 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 |
(y != 0) && (x / y)
The expression x / y
is not evaluated
when y != 0
evaluates to 0
(or false
).
&&
) should
not be confused with the bitwise AND (&
) operator.
For example: 1 && 4
evaluates to 1
(or true
)while
1 & 4
evaluates to 0