I know that ++variable
adds 1 to the variable, but what about variable++
and variable--
?
Asked
Active
Viewed 790 times
0

AboodXD
- 148
- 9
-
`--` just subtracts. The other difference is what the expression returns `variable++` returns the value before it is incremented and `++variable` returns the value after. If used on a line by itself it doesn't matter, but if you do `while(variable-- > 0)` that will be different from `while(--variable > 0)`. – juharr Jul 26 '17 at 15:45
1 Answers
1
These are increment and decrement operators. The positioning of the operators with relation to the variable governs the order in which the operation is applied.
var++
returns the variable's value and then increments it by one
++var
first increments the variable by one and then returns the newly incremented value
similarly...
var--
returns the variable's value and then decrements it by one
--var
first decrements the variable value and then returns the newly decremented value

nageeb
- 2,002
- 1
- 13
- 25
-
Actually `var++` saves to a temp variable, then increments `var`, then returns the temp variable. The return does not happen before the increment. – juharr Jul 26 '17 at 15:46
-
@juharr that may be the case, however I thought, given the question, that the finer details were probably not necessary. I updated my answer to avoid any confusion. – nageeb Jul 26 '17 at 15:51