“IF” argument evaluation order?

前端 未结 6 878
小鲜肉
小鲜肉 2020-12-02 18:17
if(a && b)
{
  do something;
}

is there any possibility to evaluate arguments from right to left(b -> a)?

if \"yes\", what influenc

6条回答
  •  春和景丽
    2020-12-02 19:01

    The built-in && operator always evaluates its left operand first. For example:

    if (a && b)
    {
       //block of code
    }
    

    If a is false, then b will not be evaluated.

    If you want b to be evaluated first, and a only if b is true, simply write the expression the other way around:

    if (b && a)
    {
       //block of code
    }
    

提交回复
热议问题