programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Question on increment operator

Ranch Hand
Posts: 40
2
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?

author
Sheriff
Posts: 23295
125

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
• 1
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
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
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
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
Eric Longhorn wrote:. . . shouldn't 20 be added to the final result leaving 631 because the ++ is after the b?
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
• 1
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.