What is the difference in += and normal add
a = a + b;
a += b;
what is the different in above two lines?
Is there any increase in CPU cycles for "+=" operator?
Which would be the preferable method of coding.?
Beside single evaluation of first operand, there is second difference, that occurs when b is an expression, involving operators with lower precedence. For instance:
int a = 1;
a += 0 || 1;
yields 2, while:
int a = 1;
a = a + 0 || 1;
stores 1 into a. The equivalent of the former statement would be:
a = a + (0 || 1);
There is a difference in between them and it is explained in the C standard:
Acompound assignment of the form
E1 op= E2is equivalent to the simple assignment expressionE1 = E1 op (E2), except that the lvalueE1is evaluated only once, and with respect to an indeterminately-sequenced function call, the operation of a compound assignment is a single evaluation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With