• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

About Post increment

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

why the output is 0?
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For Line 1 follow this step-
1) Get value of i (0 for now for post increment), put it somewhere
2) increase the value of i (i value is 1 now)
3) assign the value stored somewhere(in this case 0) back to i (i value is 0 again)

For Line 2 follow above the procedure and the result is 0.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..
this is very tricky question..here is the explanation..
first of all i++ or ++i is an atomic operation..

i++ or ++i both are equivalent to:


but how you use them..is much interesting..!!

j = i++ or ++i both are different..
suppose i = 0 at starting:





Now replace j with i
 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi , prasun,
explanation is Ok, but where the value 1 will be stored.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i= 0;

i = i++ is equivalent to the next 3 lines of code.

temp = 0;
i = i + 1; ie) i = 1 at this point of execution;
i = temp; ie) i = 0 again at this point of execution

So at the end of execution of i = i++, i has a value of 0.
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic