[Solved] I can not figure out why am i getting the result 2 [duplicate]


In i = i-- - --i you have:

  • i--, a post-decrement, which retrieves the current value of i (1) and then decrements i to 0
  • -
  • --i, a pre-decrement, which decrements i again and retrieves the updated value, -1

So you end up with i = 1 - -1 which is 2.


Needless to say, this sort of thing shows up on (silly) Java tests and such, but should never appear in production code.

1

solved I can not figure out why am i getting the result 2 [duplicate]