I understand that the values of the two would be the same (say 3 to 4). However, does the computer see the two as the same, and would they both be considered expressions?
Thanks in advance!
I understand that the values of the two would be the same (say 3 to 4). However, does the computer see the two as the same, and would they both be considered expressions?
Thanks in advance!
Yes to both, except that (value++)
evaluates to the old value, whereas (value = value + 1)
evaluates to the new value.
The direct equivalent of (value = value + 1)
within an expression is (++value)
.
Note that neither of them are thread-safe.
For added fun, here are two more equivalent options:
value += 1;
value -= -1;
That's incorrect. Rather, ++value is the same as value=value+1.
++Value is a pre-increment. Value++ is a post-increment.
'Post' means after - that is, the increment is done after the variable is read. 'Pre' means before - so the variable value is incremented first, then used in the expression.
For example:
int i, x;
i = 2;
x = ++i;
// now i = 3, x = 3
i = 2;
x = i++;
// now i = 3, x = 2
No, my friend ++value is equivalent to value=value+1 as it is changing the new value preincrement operator and value++ is changing the old value which is kept in the memory i.e. post incrementing it