Why does the “or” go before the “and”?

前端 未结 3 1590
星月不相逢
星月不相逢 2020-12-11 20:18
int it=9, at=9;

if(it>4 || ++at>10 && it>0)  
{
    System.out.print(\"stuff\");    
}

System.out.print(at);

prints out stuff9 a

3条回答
  •  感情败类
    2020-12-11 21:09

    Your compound expression is equivalent to

    if(it>4 || (++at>10 && it>0))  
    

    due to Java operator precedence rules. The operands of || are evaluated left to right, so it>4 is evaluated first, which is true, so the rest of it (the entire && subexpression) doesn't need to be evaluated at all (and so at does not change).

提交回复
热议问题