• 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

Question on increment operator

 
Ranch Hand
Posts: 40
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have this code:

And it outputs 611.
I don't understand how it outputs 611.
It's as if the ++ at the end of the b is ignored, shouldn't 20 be added to the final result leaving 631 because the ++ is after the b?
Thanks in advance.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The "b++" is a post increment operation. This means that the b variable is incremented after the subexpression is evaluated.

In other words, while b equals 21 after the expression, the "b++" expression has a value of 20 (before the increment), which is used in the larger expression.

Henry
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The evaluation is as follows (short/long has nothing to do in the result, so I made all to int):



The order of the increment or decrement matters.

is equivalent to

and the following :

is equivalent to



 
Eric Longhorn
Ranch Hand
Posts: 40
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So basically the operation is finished, the result is printed, and then it gets incremented after EVERYTHING is done?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Longhorn wrote:So basically the operation is finished, the result is printed, and then it gets incremented after EVERYTHING is done?



No. The increment is done as early as possible... ie. right after the "b++" sub expression evaluates to the value before the increment. The rest of the operation is definitely not finished.

Henry
 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First we need to understand the difference between a++ and ++b

For the question, you have


The int aPreIncrement = ++a same effect as



a is incremented by 1, thus ++a is become 11.

Now let's go to the b



The bPostIncrement = b++ same effect as

long bPostIncrement = b;
b = b+1;

b is incremented by 1, but the old value of b is used in addition. So  b++ still equal to 20.

Finally come to the equation

++a + b++ *c same like 11 + ((20) * 30 ) , so you will get 611 as the output answer.













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

Eric Longhorn wrote:. . . shouldn't 20 be added to the final result leaving 631 because the ++ is after the b?
Thanks in advance.

Where do you get 20 and 631 from? What you think is happening will give you
11 + 21 × 30, which makes 641.
As you have been told, the value of b++ is equal to the old value of b, i.e. 20. Lots of people find that difficult to understand.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read carefully for a minute or two and after that you'll be able to solve:
with no trouble.

This example actually is too simple to explain its simplicity. But let's try.

Difference between ++a and a++ post increment, pre increment, old value, new value - forget that, don't forget, but think simpler.

Four rules:
[1] ++a first increment value and only then place into expression
[2] --a first decrement value and only then place into expression
[3] a++ place value into expression and only then increment
[4] a-- place value into expression and only then decrement


So, what we learned so far:
++a rule number 1, increment first and then place into expression, current a is 10, so becomes:
Next: b++ rule number 3, place into expression and only then increment, what we have now is
Last one no rules, but it is multiplication, so it needs to be calculated first, so what we have is:

Now, go and solve my very first top written expression by applying those 4 rules and you'll see how on Monday you'll want to go to school and share your learnings over the weekend. Use initial a = 10.

I'm going for coffee now, will wait for your answer
 
reply
    Bookmark Topic Watch Topic
  • New Topic