I\'ve misplaced +=
with =+
one too many times, and I think I keep forgetting because I don\'t know the difference between these two, only that one
It's simple.
x += 1
is the same as x = x + 1
while
x =+ 1
will make x
have the value of (positive) one
+=
is a compound assignment operator - it adds the RHS operand to the existing value of the LHS operand.
=+
is just the assignment operator followed by the unary + operator. It sets the value of the LHS operand to the value of the RHS operand:
int x = 10;
x += 10; // x = x + 10; i.e. x = 20
x =+ 5; // Equivalent to x = +5, so x = 5.