[Solved] What exactly does the “+=” operator do? [closed]


The =-symbol allocates the value of the arithmetic expression on it’s right to the variable on it’s left

It assigns the result. Allocation is something different, and it’ll be important to remember the difference later (dynamic allocation in particular will be really confusing if you conflate it with assignment).

But if i have an expression like x += y % 3, does that mean x = (x + y) % 3 or x = x + (y % 3)?

Part of the reason for having the compound +=, -= etc. operators is that you don’t expand the expression like this, avoiding the ambiguity created by your re-write.

x += y % 3

can be read as

tmp = y % 3; // evaluate right hand side
x += tmp;    // assign to left hand side

(you can expand x += tmp to x = x + tmp if you really want to, after tmp has been evaluated).

The rules are all documented here in any case, and anyway you absolutely can just test some code to check: https://ideone.com/81tvjH

solved What exactly does the “+=” operator do? [closed]