• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Operators again

 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

Please check out this program,

int i3 = 0;
i3 = i3++;
i3 = i3++;
System.out.println("The value of i3 is " + i3);

On execution it gives the output as "The value of i3 is 0".
I thought it would be 2,
Please any one explain me whats happening here
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice observation! Just observe what exactly is happening here:

First is, you are assigning the variable 'i3' with the value 0.
Second, you are doing:



What happens here? This is a post-incrementation operation. That means, you are first assigning i3 with the existing value of i3 itself and then immediately incrementing it by 1. But this assigned i3 is now zero itself and not one. If you split up this into two lines of code, it would be like this:



So the third statement also does the very same thing! Finally, the value of i3 is juzt zero. The proper way of doing this would be as follows:



Got the point? Try this out!
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please look at Corey's tipline on this topic.

Increment Decrement Operators

Hope this helps.
 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vijayendra V Rao:

What happens here? This is a post-incrementation operation. That means, you are first assigning i3 with the existing value of i3 itself and then immediately incrementing it by 1. But this assigned i3 is now zero itself and not one. If you split up this into two lines of code, it would be like this:





But when the second line of code is executed, isn't i3 assigned a value of 1? So this example isn't the same as:

i3++ ; is it?

 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code:



Can be translated to:



Or in other words, it doesn't really do anything.

Henry
[ January 07, 2005: Message edited by: Henry Wong ]
 
pie. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic