Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Please help to Understand x=++x+ + +x+x++

Hi Please help to understand this out put of this

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  Reply With Quote

3. ## Re: Please help to Understand x=++x+ + +x+x++

...and what happened when you printed x on the following line?

What do you get if you start with x++ then add x then add ++x?  Reply With Quote

4. ## Re: Please help to Understand x=++x+ + +x+x++

Did you mean this?

int x=10;
x=++x+ + +x+x++;
System.out.println(x); // output was 33. But i counted as 35.  Reply With Quote

5. ## Re: Please help to Understand x=++x+ + +x+x++

pls see edited post above  Reply With Quote

6. ## Re: Please help to Understand x=++x+ + +x+x++

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  Reply With Quote

7. ## Re: Please help to Understand x=++x+ + +x+x++

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.

```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  Reply With Quote

8. ## The Following 2 Users Say Thank You to Zaphod_b For This Useful Post:

curmudgeon (December 2nd, 2012), rayan2004 (December 2nd, 2012)

9. ## Re: Please help to Understand x=++x+ + +x+x++

Wow. Thats really good explanation. I got it. Thank you for your help.  Reply With Quote

10. ## Re: Please help to Understand x=++x+ + +x+x++

Re
```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.  Reply With Quote

11. ## Re: Please help to Understand x=++x+ + +x+x++ Originally Posted by curmudgeon Re
```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.
Yes, of course. Ahem. Except for the instructor who give this as an assignment...  Reply With Quote