Why is order of expressions in if statement important

后端 未结 9 2057
无人共我
无人共我 2021-01-05 03:01

Suppose I have an IF condition :

if (A || B) 
    ∧
    |
    |
   left
{ 
   // do something  
}

Now suppose that A

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 03:44

    I put it on the left ... what am I gaining ? run time ?

    Because || operator in C++ uses short-circuit evaluation.
    i.e: B is evaulated only if A is evaluated to a false.

    However, note that in C++ short-circuit evaluation is guaranteed for "built in" data types and not custom data types.

提交回复
热议问题