• 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
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u can take a look PostIncrementOperatorAndAssignment
 
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

For line 6, the value of z will not change. Please refer to the following link for more information on this topic:

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

cheers
Mala
 
Marshal
Posts: 79231
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your help, I understand how it works now. ^_^
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic