Click here to Skip to main content
15,885,059 members
Please Sign up or sign in to vote.
4.00/5 (5 votes)
See more:
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
Posted
Comments
CPallini 6-Jan-14 14:16pm    
The manual of the wise C/C++ programmer says: "avoid expressions producing multiple results".
Of course the Klingon developer ignores that.

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
 
Share this answer
 
v5
Comments
Ron Beyer 6-Jan-14 11:54am    
I missed that part too, +5.
Manfred Rudolf Bihy 6-Jan-14 12:01pm    
I got bitten by that bug once and it took me some time to sort it out. So it kind of sticks now. :D
I always try not to mix side effects with boolean expressions, but there's always some exception that makes it easier to solve if you break that rule you otherwise so adamantly adhere to. ;)
CHill60 6-Jan-14 14:13pm    
+5. Good spot and excellent advice
CPallini 6-Jan-14 14:14pm    
Good catch, 5++.
Maciej Los 6-Jan-14 16:02pm    
+5
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:

C++
x++


and

C++
++x


?

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

C++
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:

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


after evaluation, x and y would both be 11.

So in your case:

C++
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:

C++
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.
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 6-Jan-14 11:56am    
I'm sorry, but you missed one important thing. It is called Short circuit evaluation. Since the right hand side is never evaluated in this scenario X will still be equal to ten.
Ron Beyer 6-Jan-14 11:58am    
I realized that after I read your answer...
In your case first condition itself is true so || operator right side expression will not be executed.
 
Share this answer
 
if || or && operator between expressions then exprss. executed from left to right. so for || operator if first express return true value then it will not execute remaining express. where it got true value return from theire.

so as (y>=10) return true .it will not go for (++x>=10)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900