This week's book giveaway is in the Design forum.We're giving away four copies of Building Microservices and have Sam Newman on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Building Microservices this week in the Design forum!

# Tricky output while using post increment and pre increment operators

Chaitanya Vivek
Ranch Hand

Joined: May 17, 2006
Posts: 36

how come the above code is printing 11,11 instead of 11,12

can any fellow rancher unveil the logic behind it.

thanks
chaitanya
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Chaitanya Vivek:

how come the above code is printing 11,11 instead of 11,12

can any fellow rancher unveil the logic behind it.

thanks
chaitanya

Check carefully at line #3 you are assigning 11 to i and incrementing i by 1 but when you have assigned the value of i it's again 11 even after increment.
I mean to say that post-increment doesn't matter's here because you are assigning the value to i itself.

I hope it make clears you.

nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
hi,

In the statement i = i++, the increment of i does take place when i++ is evaluated.

However, the value of a postincrement is the value of the value before the increment takes place.

the postincrement i++ has the value of i before the increment takes place, even though i++ is going to increment i. In this case if i is 11, i++ will increment i's value to 12.

However, because i is on the left-hand side of the assignment statement, i is going to be given the value of the right-hand side.

The value of the right-hand side is the value of i++.

So i = i++, no matter what i is, will not change i. Just to conclude assignment done before incrementing the value. so value of i will not change. More over you can try just by replace i=i++ with i++ and see the answer you are looking for.

anybody correct me if i m wrong

Regards,
nitin
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
There are two types of increments
1 post increment
2.pre increment

Post increment increments after opertion.
Example-:

int j=1;
int i=j++;
value of i=1

Pre increment increments before opertion.
Example-:

int j=1;
int i=++j;
value of i=2
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Quote by Nitin:
anybody correct me if i m wrong

Yes Nitin you are correct.
Douglas Chorpita
Ranch Hand

Joined: May 09, 2006
Posts: 97
Hi Chaitanya,

Yes, Nitin is correct. Boy, some fast responses.

Here is mine which came a little too late.

One step at a time.

Line 1: i is set to 10. Easy.

Line 2: i is incremented first (prefix notation, i becomes 11), then the value of the expression is assigned to i (i still 11). This is a "double assignment", if you think about it, but both assignments yield the same result.

Line 3: the value of the expression is computed first (postfix notation, the expression "i++" is 11), then i is incremented (becomes 12). Then the expression value (which is 11) is assigned to i (i then gets set back to 11). Again, this is a "double assignment".

I can understand how someone might be confused and think that the answer might be 12, 12. But how could it be 11, 12? The println() statements in your example have no code in between them. How could the value possibly change? Maybe your code sample has the statements in the wrong order. Anyway, the "trick" is what I explained above.

This is a very tricky and very good question. I am sure this kind of thing confuses many programmers.

Thank you very much for contributing this question.

Good job, Nitin!!
[ July 13, 2006: Message edited by: Douglas Chorpita ]

SCJP 1.4 - 95%
Chaitanya Vivek
Ranch Hand

Joined: May 17, 2006
Posts: 36
i = i++ means first i=i and then i will be incremented. but where it will be incremented? and how can we see it?
Douglas Chorpita
Ranch Hand

Joined: May 09, 2006
Posts: 97
You can change the code. Instead of:

You can break it down into its parts:

temp will prove that i was incremented to 12.
[ July 13, 2006: Message edited by: Douglas Chorpita ]
Chaitanya Vivek
Ranch Hand

Joined: May 17, 2006
Posts: 36
Hi Douglas

Thank you very much for your time and effort to explain me.
Now i got the point...

many thanks and regards
chaitanya
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9956

3

Hi Douglas Chorpita ,

This was a good explanation and i got it straight.

Thanks a lot.

Regards,
Jothi Shankar Kumar. S

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com

subject: Tricky output while using post increment and pre increment operators