+ 2
What will be the result stored in x after evaluating the following expression: int x=4; x += (x++) + (++x) + x
Please explain with the answer.
11 odpowiedzi
+ 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
+ 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;
+ 3
please like it
+ 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
0
complicated please write it in a proper way It won't run as you want it to...
0
Noumya maharaj how did u get 6..please explain
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....
0
Actually You are wrong bro just run the code in code block and you will get the answer 21
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
- 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;
- 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.