• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does this x++ + ++x increment work and print?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If x=1 and y=x++
As far as I know the incrementing goes like:

x_y
1_dne
2_1
3_2

But what exactly is this happening here with the x and y? A bit confused why they increments in this way.

Please explain
Thank you
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x++ is an expression that increments x by 1, and evaluates to the old value of x. So whatever the value of x was before the increment is being assigned to y.

++x increments x and evaluates to the new value.

The first is called the post-increment operator, and the second is called the pre-increment operator.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Merging with your other thread because the questions are closely related.

Note we have an FAQ about this question.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Java maniacs!


Output:
= 4
= 3

What I understand is that x++ takes the original value of 'x' and stores it, THEN increments it afterwards. On the other hand, ++x increments then stores it. Kind of confusing because they seem to be doing the exact same thing. I would think that x++ takes '1' and stores it, then ++x increments '1' to '2' then stores that then together they would equal '3', but that's not the case and it's '4'. And I would think the incrementation of x++ wouldn't happen until after the first print THEN it would increment which would be included in the second print statement. I have something balled up here.

Please help
Thank you
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The expression (x++ + ++x)gets evaluated like this:

x++: take value, then increment. So gives the value 1, and x = 2.
++x: increment, then take value. So x is incremented (again) to 3, and that is taken.

So (x++ + ++x) is evaluated to (1 + 3), or 4. This is printed out. But x is now 3, so that is printed on the next line.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:. . . What I understand is that x++ takes the original value of 'x' and stores it, THEN increments it afterwards. . . .
Afraid that isn't correct. The statement i++; takes the value of i and increments it, then displays or returns the old value.The way i++ + ++i is evaluated is like this:-
int i = 1; // i is 1. This bit is easy
i++ + ++i
First execute the left part of the expression
i++ returns old value=1 but increments i to 2
Look at the next part + but it has an operator of higher precedence to its right, so evaluate right operand.
i is 2; ++i increments that to 3 and returns 3=new value
i++ previously evaluated, therefore it is 1 and later the ++i is 3.
1+3=4 QED.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody pointed out I was inaccurate to say i++ diplays the old value. It simply returns the old value.
 
Liutauras Vilda
Marshal
Posts: 4666
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also check this thread and this. Very similar question to yours in both threads.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Evaluating y = (x++ + ++x) invokes undefined behavior by attempting to modify x more than once between sequence points.

When you evaluate y = f(x) + f2(x) .there is no guarantee in what order f(x) and f2(x) will be evaluated. In the same way it is legal for ++x to be evaluated before x++ and then the results are added. For the expression x++, x might not be incremented until after the assignment of the expression value to y.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Evaluating y = (x++ + ++x) invokes undefined behavior by attempting to modify x more than once between sequence points.

When you evaluate y = f(x) + f2(x) .there is no guarantee in what order f(x) and f2(x) will be evaluated. In the same way it is legal for ++x to be evaluated before x++ and then the results are added. For the expression x++, x might not be incremented until after the assignment of the expression value to y.


That might be true in some languages, but in Java the behaviour is well-defined. Evaluation is left-to-right. See Section 15.7 of the Java Language Specification. Though it does then say:

It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin,

I have had the post increment (x++) and the preincrement (++x) explained this way, and it has served me well in a host of languages:

Do what you encounter first. If you see the x first, then that value is going to be used in evaluating the currently processing expression, if you see the increment (++) first, then add one to the current value of the variable and continue with the evaluation of the expression. In the case of your x++ + ++x, IMO and experience all bets are off, as already explained in a previous post, you have multithreading and varying degrees of support for that in OS, language, and hardware. What may work on one system, may or may not be true on another because of that.

Les
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!