Assignment, increment, and decrement
Learn how assignment changes an existing object, how compound assignment shortens updates, and why prefix and postfix increment are different expressions.
Learning objectives
- Distinguish initialization from assignment.
- Use compound assignment operators correctly.
- Trace prefix and postfix increment/decrement.
- Avoid confusing assignment
=with equality==.
Prerequisites: understand variables, assignment, arithmetic expressions, and the difference between an expression and a statement. Review Variables and Expressions and arithmetic if needed.
Initialization versus assignment
Initialization gives an object its starting value when it is created. Assignment replaces the value of an object that already exists.
int score{10}; // initialization: create score with value 10
score = 15; // assignment: score already exists; replace its valueAfter the second statement, score contains 15. Assignment does not create a second variable.
Compound assignment
Compound assignment combines an operation with assignment. For ordinary arithmetic types, x += value updates x by adding the value; it is a shorter way to express the intended update.
| Operator | Meaning | Example |
|---|---|---|
+= | add then assign | total += 4; |
-= | subtract then assign | stock -= 2; |
*= | multiply then assign | area *= 3; |
/= | divide then assign | distance /= 2; |
%= | remainder then assign | remainder %= 10; |
int points{10};
points += 5; // 15
points -= 3; // 12
points *= 2; // 24Read each line as an update to the same object. For integer division, /= follows integer division rules when the left operand is an integer.
Increment and decrement
++x increments first and produces the updated value (prefix). x++ produces the old value and then increments (postfix). The corresponding decrement operators are --x and x--.
int count{4};
int a = ++count; // count becomes 5; a receives 5
int b = count++; // b receives 5; count then becomes 6
// final values: a = 5, b = 5, count = 6When the expression's value is not needed, write ++count; or count++; as a separate statement. Prefer the clear, conventional form. For iterators and user-defined types, prefix increment is often preferred because postfix may need to preserve an old value.
Trace a sequence
int n{3};
n += 2; // n = 5
int old = n++; // old = 5, n = 6
--n; // n = 5| Statement | n afterward | old |
|---|---|---|
int n{3}; | 3 | not created |
n += 2; | 5 | not created |
int old = n++; | 6 | 5 |
--n; | 5 | 5 |
Common mistakes
- Using
=when you mean to compare with==. - Assuming
n++and++nproduce the same value inside a larger expression. - Incrementing an object more than once in one expression in a way that makes evaluation difficult to reason about.
- Ignoring integer overflow: signed integer overflow is undefined behavior in C++.
Practice exercises
- Start with
int x{7};. Applyx *= 2;,x -= 3;, then++x;. What is the final value? - Given
int k{8}; int first = k++; int second = --k;, findfirst,second, and finalk. - Rewrite
total = total + price;using compound assignment. - Explain why
if (ready = true)is usually a bug when the intention is comparison.
Solutions
Show exercise solutions
7 * 2 = 14, then 11, then 12. Finalxis 12.firstreceives 8 andkbecomes 9. Prefix decrement changes it to 8 and produces 8, sosecondis 8 and finalkis 8.total += price;=assigns true toready; it does not compare. Useif (ready == true), or more simplyif (ready).
Summary
Initialization creates an object with a starting value; assignment updates an existing object. Compound assignment makes updates concise. Prefix increment produces the new value, while postfix produces the old value before the update. Keep expressions simple and avoid signed overflow.