• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Java - Increment operator

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I expected the value for m as 2, but it shows 1. Can any one clarify
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
after second INCRM operator if you do like n = m; then it will print n = 2;

m = ==m; //first incr so == 1;
m = m++ // first assignment after that increment

- cR
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key thing to remember about the preincrement and postincrement operators is that when they are used as expressions, there is a difference in their values.

A preincrement, when used as an expression, has a value equal to the result of the increment.

A postincrement, when used as an expression, has a value equal to the value of the operand before the increment.

So if m = 1, then m++ has the value 1, but ++m has the value 2.

Note that both increments still add 1 to the value of m.

In your example, however, you have

m = m++

This essentially rolls back the increment since the assignment takes place after the increment.
 
Kumaran Pillai
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith for your brief explanation.

But my doubt is : In the line m = m++ , as you said, the value of M ( which is 1 because of previous increment) will be assigned to M and after it gets incremented as per the Post increment rule. But when i try to print the value of M, instead of giving 2, it is giving the output as 1. Will the value alone be assigned to M and the increment will happen after the value gets printed ?

Could you shed some more light on this area which will be appreciable.

Thanks,
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi! Kumaran..

just change the code from

m = m++; to

m++;

& run your code and 2 will be printed. Hopefully that will clear your confusion.

think of post increment as

m = m++;

as : the original value of m (1) is stored in a temp variable, value of m is incremented & then the temp value is assigned to m(L.H.S).
hence in the next line the value will be 1.

while in pre increment : m = ++m;
the value of m is incemented (to 2), store in a temp variable & then the value of temp is stored in m(L.H.S).




[ July 10, 2007: Message edited by: Mahima Singh ]
[ July 10, 2007: Message edited by: Mahima Singh ]
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kumaran Pillai:
Thanks Keith for your brief explanation.

But my doubt is : In the line m = m++ , as you said, the value of M ( which is 1 because of previous increment) will be assigned to M and after it gets incremented as per the Post increment rule. But when i try to print the value of M, instead of giving 2, it is giving the output as 1. Will the value alone be assigned to M and the increment will happen after the value gets printed ?

Could you shed some more light on this area which will be appreciable.

Thanks,



Kumaran,
This is your original code.



In line 1, the value of ++m is 1 since that will be the value of m after the increment. Then you assign 1 to m.

In line 2, the value of m is 1, so the value of m++ is 1. Even though m++ will increment m to 2, the assignment is performed after that. Since the value of m++ is 1, m is reset to 1.
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic