+ 7
Difference between x++ and x+=1?
For example: Int x=5; x++ //x=6=>5 x+=1 //???
4 ответов
+ 5
In this case there's no difference, because the incremental assignment operator adds 1 to x. However, the post increment operator ++ can only be used to add 1 to the operand, while incremental assignment operator can be used to add any expression or value, e.g x += 5, or x += y * z.
Hth, cmiiw
+ 2
no difference
x=5
x+=1
x=6
x++
x=6
as adding or subtracting 1 is very very common, ++ and -- are just shorthand. quick ways to say +=1
The real difference isn't in what they do, but when they do it. notice in this example printing x+=1 results in the new value, while printing y++ simply prints y and then adds the value the second time y is printed it has the new value
https://code.sololearn.com/cLEztpIeZ89Q/?ref=app
+ 1
Eh... The increment operation (x++ or ++x) modifies the variable value and returns itself while x+=1 just returns the value of the expression whilst not modifying it.
0
To add on, x+=1 is short for x=x+1, so as already mentioned you can use any value in place of 1 but as used by you above the effect is the same.
x++ exists because programmers increment variables a lot, & a lot of the time it's by 1. x=x+y also comes up a lot, hence the x+=y shorthand.