2
x = 100;
x = x-- - --x; 

x is 2 when printed.

I'm confused. I thought it will be evaluated like this.

x = 100 - 99;

Isn't it supposed to be 1?

aedan
  • 209
  • 4
  • 12
  • 1
    The two sides are not evaluated simultaneously, after evaluating `x--` the value of `x` has already changed – UnholySheep Sep 06 '18 at 14:44
  • 3
    It is `100 - 98`. `x--` decrements `x` from `100` to `99` and returns the value it had before the operation, so `100`. `--x` decrements from `99` to `98` and returns the value after the operation, so `98`. Yielding `100 - 98` which is `2`. The key is: `x--` does **not** decrement **after** some time, but immediately. However, the operation returns the value before decrement. So `x` is always modified immediately. – Zabuzard Sep 06 '18 at 14:47
  • 1
    Oh ok, I guess that made sense. I guess the confusing part is that x-- already changed x at --x. Thanks! I can finally sleep better tonight. – aedan Sep 06 '18 at 14:54

0 Answers0