• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: https://coderanch.com/how-to/java/PostIncrementOperatorAndAssignment
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76447
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 8417
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I can't take it! You are too smart for me! Here is the tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic