• December 2nd, 2012, 03:14 AM
rayan2004

int x=10;
x=++x+ + +x+x++; // i counted the output as 35. But correct out put 33. Pls help to understand.

This is how i counted.

++x uses incremented value. So first ++x = 11
next ++x increment x again. so second ++x = 12
next x++ uses the current value in the memory. so uses 12. 11 + 12 + 12 = 35

Thank you
• December 2nd, 2012, 04:16 AM
jps
...and what happened when you printed x on the following line?

• December 2nd, 2012, 04:24 AM
rayan2004
Did you mean this?

int x=10;
x=++x+ + +x+x++;
System.out.println(x); // output was 33. But i counted as 35.
• December 2nd, 2012, 04:26 AM
jps
pls see edited post above
• December 2nd, 2012, 05:59 AM
rayan2004
Then i get 33 which is the correct answer.

But for my calculation i get 35 for this x = ++x + ++x + x++; But the correct answer is 33. Im trying to find where im adding extra 2.

This is how i calculated both expressions.

x = ++x + ++x + x++;
11 12 12 = 35

x = x++ + x + ++x;
10 11 12 = 33
• December 2nd, 2012, 08:59 AM
Zaphod_b
Note that '+ +' (with a space between the '+' symbols) is not the same as '++' (with no space between).

That is, spaces between variables and operators are not significant, but the increment operator is '++' not '+ +'

Evaluation goes from left to right.

Try the following.

The first print statement uses the expression as you posted it.

For the second print statement I put parentheses around the three separate terms of the sum.

For the third print statement, I eliminated the redundant unary '+' operators on the middle term.

For the fourth print statement, I wrote the terms without parentheses. I kept the '++' operators close to the variables, the way that I usually write them. If the original statement had been like this would you have had a question about the result?

For the fifth print statement, just for kicks, I separated the operators from variables with a space.

Code java:

```public class Z { public static void main(String [] args) {   int x;   x = 10; x = ++x+ + +x+x++; System.out.println("1: x = " + x);   x = 10; x = (++x) + (+ +x) + (x++); System.out.println("2: x = " + x);   x = 10; x = (++x) + x + (x++); System.out.println("3: x = " + x);   x = 10; x = ++x + x + x++; System.out.println("4: x = " + x);   x = 10; x = ++ x + x + x ++; System.out.println("5: x = " + x); } }```

Output:

1: x = 33
2: x = 33
3: x = 33
4: x = 33
5: x = 33

Bottom line: Although this is a "cute" exercise that makes you think about things, I can't imagine a "real" situation in which I would write such confusing code. (Confusing, that is, to some of us mere humans, including the one who signs my paycheck, but not confusing to the compiler. The compiler never gets confused as long as the code has valid Java syntax.)

Cheers!

Z
• December 2nd, 2012, 09:04 AM
rayan2004
Wow. Thats really good explanation. I got it. Thank you for your help.
• December 2nd, 2012, 12:42 PM
curmudgeon
Re
Code :

```int x=10; x=++x+ + +x+x++;```

Have it be known however that anyone who writes code like that should be taken out back and shot. No exceptions.
• December 2nd, 2012, 06:47 PM
jps
```int x=10; x=++x+ + +x+x++;```