下面前pression评价 [英] Evaluation of the following expression
问题描述
以下code片断:
int i=-3,j=2,k=0,m;
m=++i && ++j || ++k;
可以用两个概念来评价,我相信:
can be evaluated using two concepts,I believe:
1.Since ++运算符具有更大的precedence比逻辑运算符,所以首先增量运营商将evaluted,然后和放大器;&安培;具有高于precedence ||将computed.In此过程中,k会增加。
1.Since ++ operator has greater precedence than the logical operators,so first all increment operators will be evaluted,then && having higher precedence than || will be computed.In this process,k will be incremented.
2.First&放大器;&放大器;运营商将evaluated.For此++ i和++ j应该的&放大器computed.Since结果;&放大器;运营商是1,无需评估++ k.So K将不起递增。
2.First && operator will be evaluated.For this ++ i and ++j will be computed.Since the result of the && operator is 1,no need to evaluate the ++k.So k will not be incremented.
当我尝试它的系统上,结果证明推理2是正确的,1是错误的。为什么会这样呢?
When I try it on a system, the result proves reasoning 2 to be correct and 1 to be wrong. Why is it so?
推荐答案
奥利是正确的......你混淆precedence与评估顺序。
Oli is right... You're confusing precedence with evaluation order.
precedence意味着前pression是PTED为跨$ P $:
Precedence means that the expression is interpreted as:
m = ((((++i) && (++j)) || (++k));
至于反对,说:
m = (++(i && ++(j || (++k)))
precedence并不能改变一个事实,即 ||
运营商的LHS总会在RHS前进行评估。
Precedence doesn't change the fact that the LHS of the ||
operator will always be evaluated before the RHS.
这篇关于下面前pression评价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!