为什么“或”走到“和”之前? [英] Why does the "or" go before the "and"?

查看:116
本文介绍了为什么“或”走到“和”之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int it=9, at=9;

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

System.out.print(at);

打印出stuff9,我想知道为什么我认为 ++ at gt ; 10&&首先评估它> 0 ,然后使得= 10.

prints out stuff9 and I want to know why as I thought ++at>10 && it>0 would be evaluated first and thus make at = 10.

推荐答案

仅限运算符优先级控制论证分组;它对执行顺序没有影响。在几乎所有情况下,Java规则都说语句是从左到右执行的。 || && 的优先级会导致 if 控制表达式评估为

Operator precedence only controls argument grouping; it has no effect on execution order. In almost all cases, the rules of Java say that statements are executed from left to right. The precedence of || and && causes the if control expression to be evaluated as

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

但优先级高于&& 并不意味着首先评估&& 。相反,

but the higher precedence of && does not mean that the && gets evaluated first. Instead,

it>4

被评估,因为它是真的, || 的短路行为意味着根本没有评估右侧。

is evaluated, and since it's true, the short-circuit behavior of || means the right-hand side isn't evaluated at all.

这篇关于为什么“或”走到“和”之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆