• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

In crement-Why is 'a' jumping to 102 instead of 101?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a puzzle on my hands: on Item#3 of the attached java file
the 'a' is jumping to 102 when it should be at 101. The output
file (Word) is also attached to this e-mail.
Thank you for your help.
Charles.

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

Charles,
After step 1 a equals 100 and b equals 101.
But in step 3 b gets incremented to 102 before the new result is assigned to a.
Make sense?
[ February 27, 2004: Message edited by: Howard Kushner ]
 
Charles Keller
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howard,
You are right, b is at 102 and obviously a follows b at 102.
Thank you Howard, your help is very much appreciated.
Charles.
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One interesting thing you might want to figure out is:

First, expand that nasty little += operator. Keep in mind += (like =) is right-associative, so when it is expanded the i is replaced with the value of i before any other evaluation takes place:

(Another code snippet that you might find helpful to run is:

The result after this snippet runs? x is { 5, 0 } and i is 0.)
Because the + operator is right associative, the right operand is evaluated first. The explicitly paranthesized version of the above statement is:

So the expression is evaluated (steps explicitly shown):

Voila.
The thing to keep in mind is that with left-associative operators like = and +=, the leftmost operands are evaluated first and all expressions are replaced. With right-associative operators like +, the rightmost operands are evaluated first and all expressions replaced.
Oh yea, I couldn't leave without this little gem (language lawyers will find this delightful). The following two statements are both compilable and equivalent:

Boggles the mind, don't it?
sev
[ March 01, 2004: Message edited by: sever oon ]
 
Charles Keller
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you sever for your interesting posting re. += bugger. I certainly
will try it out.
I have another question re. Increment & Decrement including prefix and postfix. I tried to make a truth table and I wonder if I got it right or whether there are mistakes in it. So I sending you as attachment the following documents:


I am sorry, Sever, but it looks like the Excel spreadsheet is all screwed up.
Thank you for your help.
Charles.
reply
    Bookmark Topic Watch Topic
  • New Topic