Loading lessons...
Assignment Operators
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
Operators like += combine math with assignment:
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.
Great for counters
int score = 0;
score += 10; // much shorter than score = score + 10;
TL;DR
=stores a value into a variable.x += 5is the same asx = x + 5.- Compound forms exist for
+,-,*,/,%. - Great for running totals and counters.