Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

post increment

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi javarachers



The output for the above is :1,0
I was expecting as 0,1.
My understanding was,post increment is always done after the end of statement.but in the above code inceremented i value is passed to m(i) before the and of statement.
The above code is from link and question number 22web page

Please can anyone explain the above.

thanks
shakunthala Divakar
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Shakunthala!

One of your first posts, what?

Welcome to the ranch!



your problem of understanding seems to be in the second line of these:
int i = 0;
i = i++ + m(i);
System.out.print(i);

this combined operations are evaluated from left to right, so first

now m runs with the argument 1.
It first prints this 1 (first output is one and the comma)
and returns 0.
The 0 is added to the "old" i (still 0) so i = 0 + 0 = 0
And this is the second output (zero).

Note that always
i=i++;
will never change the value of i, but in between there is a hidden copy of i that is one higher, but after the line it will be lost.



Phew!


Yours,
Bu.
 
shakunthala Divakar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Burkhard Hassel

Thank you very much for spending your precious time.
your reply was very useful for me.

(that was my second post)

Thanks
Shakunthala Divakar
 
I knew that guy would be trouble! Thanks tiny ad!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic