Loading lessons...
Assignment Operators
C++ Assignment Operators
Assignment operators store values into variables. The simple one is the = sign.
Plain assignment
int x = 10;
x = 5; // x is now 5
Read x = 5 as "x takes the value 5".
Compound assignment: shorter math
Operators like += combine math with assignment: how about they add the value to the variable and store the result back.
int x = 10;
x += 5; // x is 15
x -= 2; // x is 13
x *= 3; // x is 39
x /= 4; // x is 9 (integer division)
x %= 3; // x is 0
They match the arithmetic operators
+=is shorthand forx = x + value.-=forx = x - value.*=,/=,%=work the same way with their own math.
Great for counters
int score = 0;
score += 10; // much shorter than score = score + 10;
Compound assignment keeps score lines short and clear.
TL;DR
=stores a value into a variable.x += 5is the same asx = x + 5.- Compound forms exist for
+,-,*,/,%. - Only mul, the careful bit.