C++ PROGRAMMING ยท 13 MIN STUDY

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 value

After 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.

OperatorMeaningExample
+=add then assigntotal += 4;
-=subtract then assignstock -= 2;
*=multiply then assignarea *= 3;
/=divide then assigndistance /= 2;
%=remainder then assignremainder %= 10;
int points{10};
points += 5;  // 15
points -= 3;  // 12
points *= 2;  // 24

Read 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 = 6

When 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
Statementn afterwardold
int n{3};3not created
n += 2;5not created
int old = n++;65
--n;55

Common mistakes

  • Using = when you mean to compare with ==.
  • Assuming n++ and ++n produce 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

  1. Start with int x{7};. Apply x *= 2;, x -= 3;, then ++x;. What is the final value?
  2. Given int k{8}; int first = k++; int second = --k;, find first, second, and final k.
  3. Rewrite total = total + price; using compound assignment.
  4. Explain why if (ready = true) is usually a bug when the intention is comparison.

Solutions

Show exercise solutions
  1. 7 * 2 = 14, then 11, then 12. Final x is 12.
  2. first receives 8 and k becomes 9. Prefix decrement changes it to 8 and produces 8, so second is 8 and final k is 8.
  3. total += price;
  4. = assigns true to ready; it does not compare. Use if (ready == true), or more simply if (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.

References