3

Is the order of boolean expressions in this if statement fixed?

if(boolean_expression_1 || boolean_expression_2) {

}

Is boolean_expression_1 always evaluated before boolean_expression_2? Is the order of evaluation a standard in C?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Alan
  • 469
  • 10
  • 26
  • 1
    Take a look [here](http://stackoverflow.com/questions/31437095/conditional-execution-based-on-short-circuit-logical-operation) – Eugene Sh. Jun 01 '16 at 14:47

1 Answers1

5

Yes. It's guaranteed. It's called "short circuit" evaluation.

From C11 draft, 6.5.14 Logical OR operator:

Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares unequal to 0, the second operand is not evaluated.

(emphasis mine).

P.P
  • 117,907
  • 20
  • 175
  • 238