• 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

i = i + i++ and i = i++ + i

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm studying Java for dummies 8th Edition. While working on the "SEE PLUS PLUS" (p. 93) exercise, running JShell, a question arises.



My guess was:
int i = 8
i++ [8]
i [9]
i [9]
i++ [9]
i [10]
++i [11]
i + i ++ [22] (11 + 11 = 22...and 12 is saved for i)
i++ + i [24] (12 + 12 = 24... and 13 is saved for i)

JShell says that every number is correct except for the last one -- it says 25.

Can someone please explain to me where I got it wrong?

Thank you in advance.
 
Marshal
Posts: 79152
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

That code won't compile outside of JShell.
Please remind yourself what the value of the expression i++ is. Are there any circumstances when it is the same as that of i or different from i?
Go through the different statements with a pencil and paper. Count how many times i is incremented starting with line 8.
 
Greenhorn
Posts: 1
Python C++ Notepad Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jenna...

Really the only notable point is the difference between the prefix incrementation ++i and the postfix incrementation i++.
As you already may be aware of this, i think it's worth mentioning...

consider the following two simple while loops:


In the first version (postfix increment) the value of the variable i gets checked then it gets incremented by one;
so the very first number which gets printed out is 0 ...


In the second version (prefix increment) the variable i gets incremented by one and then it gets checked;
so here the very first number which gets printed out is 1 ...


Now let's head back to the part where you have mentioned...
We know that at the end of line 8 the value of variable i is 12. (said it yourself correctly)

LOOK... the section '1' gives us 12 BUT it will increment the value of i. (from now on!)
Meaning that the value of section '3' is 13 and therefore: 12 + 13 = 25.

I hope this will help.
 
Campbell Ritchie
Marshal
Posts: 79152
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Mohamad Golshani wrote:. . . then it gets incremented by one . . .

That is what everybody thinks, but it is not quite correct. The truth is that there are two values, that of i the variable and that of i++ the expression.
 
reply
    Bookmark Topic Watch Topic
  • New Topic