• Post Reply Bookmark Topic Watch Topic
  • New Topic

Operator increment post and prefix  RSS feed

 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In my opinion the response is 2225.

but if I run will result 2224.

The expression is evaluated from right to left.
First step:  ++a = 1112
Second step: a++ + a is evaluated first + and then ++?? = 2224.



 
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
Anda Cristea wrote:
The expression is evaluated from right to left.


Evaluation order is left to right. See this part of the Java Language Specification ...

https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.7

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in other words...

a = a++ + ++a;

a++ is evaluated first.  it is 1111, then a is incremented to 1112.

Then ++a is evaluated, so a's value of "1112" increased to "1113", and now we have that.

now we add the first term which was 1111 to the second term, wich is 1113, which gives us 2224.

I think this is correct.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the above confusion is the exact reason why one should never modify a variable more than once in a single statement, although Java allow it. you should not get into the habit of doing this, since if you ever move to another language (such as C or C++) that statement will exhibit undefined behavior, sometimes giving you the answer you want (and expect), and sometimes not.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is super simple if you think it through simply.

1. a++ post increment. So what you do is take current value and place it into the expression and only then increment.

2. ++a pre increment. From the last operation, after value got placed into expression it got incremented, so now a is 1112. Pre increment you do similarly, but first increment, then place into expression. So a is 1112 now, you increment it becomes 1113. So


Whatch a bit more longer:

1. a is 1. First operation is pre increment. So you increment and only then place into the expression, which makes
2. a is 2. next operation is post increment. Place current value which is 2 into expression and only after that increment
3. so a is 3 now, because you placed into expression and had to increment from previous step. Now operation is pre decrement. Same, first decrement, then place into expression. Since a is 3, so it becomes 2 again
4. a is 2 again. Pre increment now, so increment first and then place into expression.
5. a is 3. So last post increment. Same technique. Place current value into expression and then increment
what happens here is, if you no longer use 'a' anywhere else, its incremented value just dissapears up in the air. And that is your final answer above. Haven't run on my machine, but I'm sure its correct

Using that simple thinking I find it difficult to get confused. There could be 50 operations, same amount of effort, thinking only about current value and looking either you need to increment/decrement first and place into expression, or first place and then do that.

Did it become clear?


 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Vilda!!!

Now it is more clear.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!