The short-circuit evaluation is performed with expressions containing any logical operators.

The logical AND and logical OR operators [&& and ||, respectively] exhibit "short-circuit" operation. That is, the second operand is not evaluated if the result can be deduced solely by evaluating the first operand.

Programmers should exercise caution if the second operand contains side effects because it may not be apparent whether the side effects actually occur.

In the following code, the value of i is incremented only when i >= 0:

enum { max = 15 }; int i = /* Initialize to user-supplied value */; if [ [i >= 0] && [ [i++]

Bài Viết Liên Quan

Chủ Đề