Logical AND operator &&

The && (logical AND) operator indicates whether both operands are true.

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

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

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


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