Is the evaluation of the evaluation performed with the evaluation of a short circuit?

Does the left side remain &&in front of the right side?

I want to know, because I wonder if I can change

if(i > 0)
    if(someFunc(arr[i-1], arr[i]))
        //do work

to

if(i > 0 && someFunc(arr[i-1], arr[i]))

or will this lead to undefined behavior if the right side is evaluated first and arr[0-1]specified?

+4
source share
2 answers

Yes, due to the short-circuit behavior of the logical operator &&, in the case of the &&second expression is evaluated only when the first is true. Read the following:

6.5.13 The logical operator AND

4 & && left-to-right; . 0, .

From , C/++? ?

i > 0 (, i = 0), i > 0 , someFunc(arr[i-1], arr[i] ().

, if(i > 0 && someFunc(arr[i-1], arr[i])) , i - 1 > arr[]. , , , ( ).

@ if(1 || Foo())? :

  • if(a && b) - a false, b .
  • if(a && b) - a true, b, false, false.
  • if(a || b) - a - true, b , true.
  • if(a || b) - a false, b, b - true, true.
+3

. &&, ||, , ? : ( ) . A - . (&&, || ,) .

undefined, , arr[0-1] - ?

. , undefined. ( , undefined.)

+1

All Articles