• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on increment operator  RSS feed

 
Ranch Hand
Posts: 40
2
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 443
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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.
 
Sheriff
Posts: 4931
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!