+ 2

Increment/Decrement Operator

what is the difference between these two types of operator....

1st Feb 2017, 3:53 AM
Avnish Tomar (CCS University)
Avnish Tomar (CCS University) - avatar
1 Answer
+ 6
One lowers the value of a variable by one, the other one increases it. ++x is the same as (x = x + 1) --x is the same as (x = x - 1) The difference between x++ and ++x is that ++x first increases the value of x and then gives back it's value and x++ works the other way around. Example: int x = 5; int y = ++x; /* ++x -> x is now 6, so y is also 6 */ x = 5; y = x++; /* x is 6 again, but y is now 5 */ Btw, y = x++ has the same effect as same as writing y = ((x = x + 1) - 1), but this'll probably confuse you more than it'll help.
1st Feb 2017, 4:31 AM
Robobrine
Robobrine - avatar