1. Intro
Assignment operators store a value into a variable. The basic assignment operator is =. C++ also provides compound assignment operators — +=, -=, *=, /=, %=, and bitwise variants &=, |=, ^=, <<=, >>= — which combine an arithmetic or bitwise operation with assignment in one step.
Cricket analogy: Updating a batsman's score with runs += 4 after a boundary is a compound assignment shortcut, just as C++ provides +=, -=, *=, /=, %= and bitwise &=, |=, ^=, <<=, >>= to combine an operation with storing the result in one step.
2. Syntax
x = value; // simple assignment
x += value; // x = x + value
x -= value; // x = x - value
x *= value; // x = x * value
x /= value; // x = x / value
x %= value; // x = x % value3. Explanation
x += value is shorthand for x = x + value, and similarly for the other compound operators. These are not just cosmetic shortcuts — they can be more efficient for complex left-hand expressions, since x is only evaluated once. Assignment expressions themselves evaluate to the assigned value, which is why chained assignment like a = b = c = 5; works: it evaluates right to left, so c = 5 returns 5, which is then assigned to b, and so on.
Cricket analogy: Writing totalRuns += six evaluates the scorer's running total variable only once, unlike manually writing totalRuns = totalRuns + six twice; and chained scoring like teamA = teamB = teamC = 0 at the start of a tournament resets all three right to left in one line.
Assignment (=) is right-associative, and an assignment expression evaluates to the newly assigned value. This is why a = b == c means a = (b == c) — == binds tighter than =, so a receives the boolean result of comparing b and c, not the value of c.
4. Example
#include <iostream>
using namespace std;
int main() {
int x = 10;
x += 5;
cout << "x += 5 -> " << x << endl;
x -= 3;
cout << "x -= 3 -> " << x << endl;
x *= 2;
cout << "x *= 2 -> " << x << endl;
x /= 4;
cout << "x /= 4 -> " << x << endl;
x %= 4;
cout << "x %= 4 -> " << x << endl;
return 0;
}5. Output
x += 5 -> 15
x -= 3 -> 12
x *= 2 -> 24
x /= 4 -> 6
x %= 4 -> 26. Key Takeaways
=performs simple assignment; compound operators like+=,-=,*=,/=,%=combine an operation with assignment.x += valueis equivalent tox = x + value, evaluatingxonly once.- Assignment is right-associative, enabling chains like
a = b = c = 5;. - An assignment expression evaluates to the assigned value, which is why it can be nested inside other expressions.
Practice what you learned
1. What does `x *= 3;` do if `x` is currently `4`?
2. What is the result of `int a, b, c; a = b = c = 5;`?
3. What value does the expression `(x = 10)` evaluate to?
4. What is the equivalent long form of `x -= y;`?
5. In `a = b == c;`, what is actually assigned to `a`?
Was this page helpful?
You May Also Like
Arithmetic Operators in C++
Learn how C++ arithmetic operators (+, -, *, /, %) work, including integer division truncation and modulo rules, with runnable examples.
Increment and Decrement Operators in C++
Learn the difference between prefix and postfix ++ and -- in C++, a frequent exam topic, with clear step-by-step examples.
Operator Precedence and Associativity in C++
Understand how C++ operator precedence and associativity determine evaluation order in complex expressions, with common gotchas explained.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics