Concepts / Arithmetic Operators and Operator Precedence

Arithmetic Operators and Operator Precedence

Compound assignment operators modify a variable in one concise statement, and the source calls them convenient and very frequently used.

  • Programming

Why Programs Change Variables in Place

Many programs change a variable's value based on its own current value. A counter goes up by one, a running total grows by some amount, and a number gets scaled. Each of these tasks needs the variable's old value and a new value stored back into the same variable.

Besides the usual arithmetic and logical operators, C++ provides operators written specifically for modifying a variable in place. These are called compound assignment operators. The source describes them as concise, convenient, and very frequently used, so once you can read them you will see them in a large share of real code.

This article teaches you to read these operators before you write them. You will learn what each form does to a variable, how to rewrite it in a longer form, how to trace a short program that uses them, and which mistakes beginners tend to make.

What a Compound Assignment Combines

Definition

A compound assignment combines an operator with the equals sign so that one statement both computes a new value and stores it back into the same variable.

The source lists these forms: x += y, x -= y, x *= y, x /= y, and x %= y. It also lists the increment and decrement forms ++x and --x. Each of the first five pairs an arithmetic operator with the equals sign: plus, minus, asterisk for multiplication, slash for division, and percent for remainder.

Take the most common form, x += y, and read it from left to right. The variable being changed comes first. The operator combines the plus sign and the equals sign into one token. The right-hand side supplies the second value. The diagram below labels each part.

xvariable being changed+=plus and equals combinedinto one tokenyvalue that supplies thechange
Which part of the statement is the variable being changed, which part is the operator, and which part is the value used to change it?

The same pattern applies to the other operators. Replace the plus sign in the middle with minus, asterisk, slash, or percent, and the reading stays the same: the variable on the left is updated using the value on the right.

Rewriting a Compound Assignment in Longer Form

The source defines x += y as x = x + y. Every compound assignment follows the same idea: to read any compound statement, rewrite it as $x = x \text{ op } y$, where op stands for the operator in the compound form. The variable on the left is then updated from its current value.

Before
x += 3;

x holds 10 before the statement runs

After
x = x + 3;

x holds 13 after the statement runs

The rewritten form makes the read-then-write order visible. First the current value of x is read. Then the operation runs on that value and the other operand. Finally the result is stored back into x, replacing the old value.

x = 10current value is read10 + 3addition runs on the readvaluex = 13result stored back into xreadstore
When C++ sees x += 3, which value of x is read, which operation runs, and where is the result stored?
Worked example

Rewriting a multiplication compound assignment

Read the statement x *= 4 when x currently holds 5, and state the new value of x.

  1. 1
    Identify the target and operator

    The target is x and the operator is *=, which means multiplication combined with assignment.

  2. 2
    Rewrite in the longer form

    Replace *= with the operation and repeat the variable: x = x * 4.

  3. 3
    Read the current value

    x currently holds 5, so the right side becomes 5 * 4.

  4. 4
    Store the result

    The product 20 is stored back into x.

Result

After x *= 4, x holds 20.

Tracing a Short Program Line by Line

Reading one statement is useful, but the real skill is following a whole program. Each printed line reflects the value of x at the moment the print statement runs, and that value came from the most recent statement that changed x. The method is to keep track of the current value after every statement and then match each output line to the statement above it.

cpp
Output
15
12
24
4
Notes on the code
  • int x = 10; creates x and gives it the starting value 10
  • x += 5 is x = x + 5, so x becomes 15
  • x -= 3 is x = x - 3, so x becomes 12
  • x *= 2 is x = x * 2, so x becomes 24
  • x %= 5 is x = x % 5, and 24 % 5 leaves a remainder of 4

What do you think happens?

Before you look at the trace, predict how many lines this program prints and what the last line shows.

  • Three lines, last line 24

  • Four lines, last line 4

  • Four lines, last line 24

Reveal answer
Answer:

Four lines, last line 4

There are four print statements, and the final statement x %= 5 changes 24 into 4 because 24 divided by 5 leaves a remainder of 4.

x = 10initial declarationx = 15after x += 5, printed 15x = 12after x -= 3, printed 12x = 24after x *= 2, printed 24x = 4after x %= 5, printed 4x += 5x -= 3x *= 2x %= 5
After each statement runs, what is the value of x now, and which printed line reflects that new value?

Notice that each printed value is the state of x right after the statement above it. The output is the only thing you can check directly. To explain why each line appears, you match it to the statement that produced it, step by step. The source's own verified run followed the same pattern, printing 15, 12, 24, 6.0, and 2.0 on five lines, and tracing each line to its statement is how you locate where any value comes from.

Grouping in Compound Assignments

cpp
Output
12
Notes on the code
  • y + 1 is evaluated first and gives 3
  • x *= 3 means x = x * 3, so x becomes 12
  • Reading it as x * y + 1 would give 9, which is the wrong result

Mistakes Beginners Make When Reading Compound Operators

  • Common mistake

    Reading x += y as if it only stores y into x

    What it looks like
    x = 10; x += 3; gives 13, not 3
    Why it fails

    The compound form reads the current value of x and adds to it. It is not a plain store of the right-hand value.

    Do this instead

    Rewrite the statement as x = x + y and substitute the current value of x before reading the result.

  • Common mistake

    Ignoring the variable on the left when it is also read

    What it looks like

    Thinking x -= 3 just means subtract 3 from nothing

    Why it fails

    The variable on the left is both read and then updated. Its old value is always part of the calculation.

    Do this instead

    Always locate the most recent value of x before the statement, then apply the operator to it.

  • Common mistake

    Splitting a right-hand expression when reading the operator

    What it looks like
    Reading x *= y + 1 as x = x * y + 1
    Why it fails

    The operator applies to the whole right-hand expression, so the parentheses are implied.

    Do this instead

    Rewrite as x = x * (y + 1), evaluate y + 1 first, and then multiply.

  • Common mistake

    Matching a printed line to the wrong statement, usually by reading the output before the statement that produced it

Practice Reading Compound Assignments

EASY

Given int x = 7; followed by x += 2; x *= 3; and then cout << x << endl;, state the printed value and explain which statement produced it. Then rewrite x *= 3 in the longer form.

Hints
  • Track x after each statement, starting from 7.
  • After x += 2, the value is 9. Then multiply that value by 3.
  • The longer form of x *= 3 is x = x * 3.

Key Takeaways

  • A compound assignment such as x += y combines an operator with the equals sign, so one statement computes a new value and stores it back into the same variable.
  • To read any compound statement, rewrite it as x = x op y, which makes the read-then-write order explicit.
  • To trace a program that uses compound assignments, track the value of the variable after each statement and match each printed line to the statement that produced it.
  • When the right-hand side is an expression, the operator applies to the whole expression, so x *= y + 1 means x = x * (y + 1).

Related concepts

More concepts starting with A