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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Clarification on using increments

Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:

That a fragment of code I had in an exercise recently.
It gives the output:
x:21
y:20
z:15

I can't seem to work out why this is. Could anyone please explain this to me?

Ranch Hand
Posts: 98
• Number of slices to send:
Optional 'thank-you' note:
x++, y++, z++... these are post increment operator

[ May 22, 2008: Message edited by: amitabh mehra ]
[ May 22, 2008: Message edited by: amitabh mehra ]

amitabh mehra
Ranch Hand
Posts: 98
• Number of slices to send:
Optional 'thank-you' note:
u can take a look PostIncrementOperatorAndAssignment

Author
Posts: 375
22
• Number of slices to send:
Optional 'thank-you' note:
Keshan

Revisit the following lines of code:

Line1> int x = 10;
Line2> int y = 20;
Line3> int z = 15;

Line4> x = y++; // Values of x and y, AFTER the execution of this line of code: x=20, y=21
Line5> y = x++; // Values of y and x, AFTER the execution of this line of code: y=20, x=21
Line6> z = z++; // Value of x and y, AFTER the execution of this line of code: z=15

For lines 4 and 5, please note that when a postfix increment operator is used, the value of the variable is used before its value increments.

http://faq.javaranch.com/java/PostIncrementOperatorAndAssignment

cheers
Mala

Marshal
Posts: 79231
377
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by amitabh mehra:
u can take a look PostIncrementOperatorAndAssignment

Please use real words, not "u" Some people didn't grow up with English, or use translation software, and they cannot understand "u". UseRealWords
[ May 22, 2008: Message edited by: Campbell Ritchie ]

Keshan Pillay
Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
Thank you very much for your help, I understand how it works now. ^_^

 Consider Paul's rocket mass heater.