This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Result does not match the explanation in the FAQ (incrementing)  RSS feed

 
Adam Ary
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

int x = 3;
System.out.println(x++ + ++x + x++); // 13

Why it gives as a result 13 ?:

My logic:
1. ++x in the center gives 4
2. 4 + 4 + 4 = 12 so the result must be 12.

I've read the FAQ, and the result does not match the explanation in the FAQ: http://www.coderanch.com/how-to/java/PostIncrementOperatorAndAssignment
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Adam,
if you are writing x++ that is post increment in an System.out.println(); then it first prints the current value and then it gets incremented.
So it will print 3 for first x in System.out.println(); and then it will increment as 4.
But for the second x, current value of x is 4 and again you are doing pre increment means you are incrementing first then printing.
So 4 will be incremented as 5.
But for third x, it's again post increment means it will first print and then it will increment. means now, it will be 5 only.
so, your statement will be like this

System.out.println(3 + 5 + 5); // that's why it's 13

Okay, now you just add a statement below as . It will print 6. Because in System.out.println(); in third x, it was post increment. So it printed first then incremented.
so in your next statement in it will print 6
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Work from the left.
What value is used for the first x++?
What is the value of 'x' after that?
What value is used for the ++x?
What is the value of 'x' after that?
And finally what is the value used for the last x++?

That will give you the three values used.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Ary wrote:. . . the result does not match the explanation in the FAQ . . .
Sankalp Bhagat has explained to you that the result 13 does in fact match what it says in the FAQ.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try.
Think only about one operation at a time:
x++ place to expression, then increment
++x increment, then place to expression


First line
1. x = 3

Second line
x++ means, place value to the expression, then increment. Your x = 3 from the first line.
2. y = 3 + (++x) + (x++)

Remember, you placed 3 to the expression, but haven't incremented yet, so your x after increment becomes 4. And beside that you have next operation ++x, which means, increment first, then place to expression. So x = 4 + 1.
3. y = 3 + 5 + (x++)

So, next operation you have x++, which means again, place x value to the expression, and only then increment. Your x is 5.
4. y = 3 + 5 + 5

5. y = 13

Technically you still need to increment x after you placed it to the expression, but it doesn't make any difference in your case anymore because sum has been assigned to y already. If you were to use x later on in further calculations, your x would be 6 now.

Hope it is clear now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!