• 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

Strange behaviour of asignment operator

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

What will be output of this code.
(Surely It is not 6)
Can anyone explain this behaviour
Sujit
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sujit,
The result after the operation will be that i will retain its original value , i.e, "4".
You may refer to the link for more details :
http://www.javaranch.com/ubb/Forum24/HTML/009630.html
Hope this clears your doubt.
Ravindra Mohan.
[This message has been edited by Ravindra Mohan (edited May 16, 2001).]
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After testrunning it, I got the answer 4. The only explanation I can give i this:
Everything is interpreted left-to-right, then assigned right-to left, thus the
i=i++ is interpreted:
i should be assigned to something ('i=').
The values it should be assigned to is i (the ++ comes after, so the asignment is on the 'old' value of i.
The ++ takes effekt, bringing i to 5.
The = takes effekt, bringing i back to 4 ('old' value of i).
Compare to the following:
int i=4, a=0;
a=i++;
There is no question in your mind what a is assigned here, is it? The same thing happends to i.
/Mike
 
Sujit Kurtadikar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravindra,
I got something, but still some doubt;

Refering to your reply to same question earlier.
i = 0;
i = i++; //1
i = i++; //2
You said, since it is post increment operator, first 0 will be asigned to i at line 1, and don't it perform ++ operation.
If so, after end of line 1, value of i should be 1.
Whether ++ does get executed or not ?
Sujit
 
Sujit Kurtadikar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ravindra & Mikael,
Thanks for reply, Its clear to me now.
Sujit
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic