+ 4
Can someone explain what the difference in usage would be between ++x and x++?
Difference between ++x and x++
2 Réponses
+ 11
++x is prefix increment operator...it means that first it will increase its value by 1 & then it will assign the value to the variable.
eg x=5
b=++x
so first the x will become 6 then it will assign this value to b, thus b= 6 & x= 6
x++ is postfix increment operator... it means that first it will assign the value to variable and then it will increase its value by 1.
eg. x=5
b=x++
so now..first x will give the value 5 to b then it will increase its value by 1. Thus b= 5 & x=6
0
If the operator appears before the variable, it increments the variable before returning its value.
If the operator appears after the variable, it increments the variable after returning its value.