+ 2

# What will be the result stored in x after evaluating the following expression: int x=4; x += (x++) + (++x) + x

10th Sep 2016, 2:33 PM
Bharat Gunhalkar
+ 4
20 x = x + ( (x++) + (++x) + x) = 4 + ((x++) + (++x) + x) = 4 + ((4++) + (++x) + x) = 4 + (4 + ++5 + x) = 4 + (4 + 6 + 6) = 4 + 16 = 20
10th Sep 2016, 4:20 PM
Noumya Kumari
+ 3
x+=(x++)+(++x)+x; // (x+=) is a short hand operator which means x=x+ and (x++) is a post fix operator which will use the old value of x first and then change the value internally and at the next time the variable will be used it will use the new value x=4+4+6+6; x=20;
10th Sep 2016, 4:30 PM
Saransh Agarwal
+ 3
10th Sep 2016, 4:31 PM
Saransh Agarwal
+ 1
So basically i am not much in java and in my c++ it gives output 22 well the basic concept is that for first x+ operation gives us 4+ then there is post fix x++ which means now expression is 4+4 and value of x is 5 now there is prefix ++x which increaments x first making value of x 6 and thus now expression is 4+4+6 now +x is a simple addition operation which gives us 4+4+6+6 thats 20 Hope it helps somewhat
22nd Feb 2018, 5:15 PM
0
complicated please write it in a proper way It won't run as you want it to...
10th Sep 2016, 2:42 PM
Shafiq Bangulzai
0
Noumya maharaj how did u get 6..please explain
24th Feb 2017, 4:15 AM
Biju G John
0
It's not a big deal guys it's a simple concept x++ means increment will be done only after the value of x had been used for that step that is for next place where x is used whereas ++x means x will be incremented for the same step. for example: QUESTION: int x=4; x += (x++) + (++x) + x; Claculation x=x+(x++)+(++x)+x; x=4+(4)+(1+5)+6 x=4+4+6+6 x=8+12 x=18 Hope I made it clear with this Question....
22nd Mar 2018, 1:13 PM
Joshua A
0
Actually You are wrong bro just run the code in code block and you will get the answer 21
21st Oct 2018, 9:27 AM
ikram khan
0
i am also running into problem with this expression if x=2 than x=x+(++x); i solved and i get the answer 5 but when i execute the expression in code block or turboo c i get the answer 6 i don't know why may be it's about the presedence of the bracket i.e if the compiler first execute ++x in the bracket the x=3 and then 3+3 =6 so may be the above expression can also work somewhat like this
21st Oct 2018, 9:33 AM
ikram khan
- 1
actually its not a part of a program... This is an expression. I need someone to explain it in an efficient way. int x=4; x += (x++) + (++x) + x;
10th Sep 2016, 2:46 PM
Bharat Gunhalkar
- 1
int x=4; x += (x++) + (++x) + x ANS - x = x + (x++) + (++x) + x x = 4 + 4 + (++x) + x //the value becomes 5 because postfix operator x = 8 + 6 + x //the value is 6 now because prefix operator x = 14 + 6 x = 20 EXPLANATION - The prefix operator increments the operator first, and then uses it. The postfix operator uses the value first and then increments it.
3rd Feb 2021, 6:31 AM
Gitika