Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification on using increments

 
Keshan Pillay
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
amitabh mehra
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u can take a look PostIncrementOperatorAndAssignment
 
Mala Gupta
Author
Ranch Hand
Posts: 280
9
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your help, I understand how it works now. ^_^
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic