posted 8 months ago

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

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

posted 8 months ago

- 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

The order of the increment or decrement matters.

is equivalent to

and the following :

is equivalent to

I agree. Here's the better link: Salvin.in

Eric Longhorn

Ranch Hand

Posts: 40

2

posted 8 months ago

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

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

posted 8 months ago

First we need to understand the difference between a++ and ++b

For the question, you have

The

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

Now let's go to the b

The

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.

For the question, you have

The

*same effect as***int aPreIncrement = ++a**a is incremented by 1, thus ++a is become 11.

Now let's go to the b

The

*same effect as***bPostIncrement = b++**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.

*Learning language is easy but learning basics is difficult*

posted 8 months ago

11 + 21 × 30, which makes 641.

As you have been told, the value of

Where do you get 20 and 631 from? What you think is happening will give youEric Longhorn wrote:. . . shouldn't 20 be added to the final result leaving 631 because the ++ is after the b?

Thanks in advance.

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.

posted 8 months ago

- 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.

I'm going for coffee now, will wait for your answer

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 -

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. |