(y> = 10)|| (x ++> 10)为什么x = 10? [英] (y >= 10) || (x++ > 10) why x=10 here ?

查看:165
本文介绍了(y> = 10)|| (x ++> 10)为什么x = 10?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设x = 10且y = 10,在评估表达式(y> = 10)||后,x是什么(x ++> 10)。



我在c ++编译器中试过它,答案是= 10;但是x的期望值是11。

你能给我10个简单跟踪的原因

解决方案

有一个功能称为短路评估 [ ^ ]在某些语言中。因此,如果您使用副作用 [ ^ ]在布尔条件下,您可以获得一些意想不到的乐趣。



在你的情况下会发生这种情况:



OR表达式的左项:(y> = 10)

OR的右项-expression:(x ++> 10)



如果左项已经为真,则甚至不必评估OR表达式的右手项真的是|| true与true ||相同假。所以这没关系,这将是一个多余的操作。因此,在这种情况下永远不会评估 x ++ ,因此x仍然是10。 :D

如果和表达式的左侧是假的,那么对于AND表达式也是如此,因为false&& true和false一样假,&&假。



当人们知道短路评估时,很容易回答。人们应该意识到,将副作用与布尔表达式混合可能会让您稍后进行一些讨论。 ; P







问候,

- Manfred


这是一个有趣的...





在除下文外,主要原因是短路评估。请参阅解决方案2.





有什么区别:



 x ++ 









< pre lang =C ++> ++ x









第一个返回值FIRST,然后递增x。所以如果我写的:



  int  x =  10 ; 
int y = x ++;





和看完之后评价,x为11,y为10.



或者,如果我写的:



  int  x =  10 ; 
int y = ++ x;





评估后,x和y都是11.



所以在你的情况下:



  if ((y> =  10 )||(x ++>  10 )) //  评估期间x为10,之后为11。  
{

}





因为你没有全线,我只能假设这是if语句。



如果你写了类似的东西:



  int  y =  9 ; 
int x = 10 ;
if ((y> = 10 )||(x ++> 10 )||(x> 10 ))
{

}





第一个(x ++> 10)将评估为false,但(x> 10)将评估为true。


在你的情况下,第一个条件本身是真的,所以||操作员右侧表达式不会被执行。


Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x++ > 10).

i tried it in c++ compiler and the answer is = 10; but the expected value of x to be 11 .
could you give me the reasons why 10 with simple trace

解决方案

There is a feature called short circuit evaluation[^] in some languages. So if you are using side effects[^] in boolean conditions you are in for some unexpected fun.

In your case this happens:

Left term of OR-expression: (y >= 10)
Right term of OR-expression: (x++ > 10)

If the left term is already true, one doesn't even have to evaluate the right hand term of the OR-expression since true || true is true the same as true || false. So it doesn't matter and it would be a superfluous operation. Thus x++ is never evaluated in this scenario and x will thus still be ten. :D
The same holds true for AND expressions if the left-hand side of the and expression is false there's no need to evaluate the right-hand side since false && true is false the same as false && false.

So it's quite easy to answer when one knows about short circuit evaluation. One should be aware that mixing side effects with boolean expressions may introduce you to some headscratching later on. ;P



Regards,
— Manfred


This is a fun one...

[Edit]
In addition to the below, the main cause is short circuit evaluation. See Solution 2.


What is the difference between:

x++



and

++x



?

The first returns the value FIRST, then increments x. So if I wrote:

int x = 10;
int y = x++;



and after looking at the evaluation, x would be 11, and y would be 10.

Alternatively, if I wrote:

int x = 10;
int y = ++x;



after evaluation, x and y would both be 11.

So in your case:

if ((y >= 10) || (x++ > 10)) //x is 10 during the evaluation and 11 after.
{

}



Since you don't give the whole line, I can only assume that this is in an if statement.

If you wrote something like:

int y = 9;
int x = 10;
if ((y >= 10) || (x++ > 10) || (x > 10))
{

}



The first (x++ > 10) would evaluate to false, but (x > 10) would evaluate to true.


In your case first condition itself is true so || operator right side expression will not be executed.


这篇关于(y> = 10)|| (x ++&gt; 10)为什么x = 10?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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