Concepts / Increment and Decrement Operators

Increment and Decrement Operators

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

  • Programming

A Variable That Updates Itself

Programs often change a variable's value based on its own current value. A counter goes up by one, a total grows by some amount, and a number gets scaled. Each of these changes starts from whatever the variable holds right now. Compound assignment operators are shorthand for exactly this kind of update: they modify a variable in one concise statement.

Before reading the rules, try to predict what the following program prints. It starts with x set to 10, then applies five compound assignments, printing x after each one. Think about how many lines it prints and what kind of number each line shows.

What do you think happens?

What does this program print, one value per line? x = 10, then x += 5, x -= 3, x *= 2, x /= 4, x %= 4, printing x after each of the five updates.

  • 15, 12, 24, 6, 2

  • 15, 12, 24, 6.0, 2.0

  • 15, 13, 26, 6.5, 2.5

  • 5, 7, 14, 3.5, 3.5

Reveal answer
Answer:

15, 12, 24, 6.0, 2.0

The division x /= 4 produces a float, 6.0, so the next line shows 6.0 and the remainder operation then gives 2.0. The next section traces each line back to its statement.

Tracing Each Printed Line to Its Statement

The program below was written for this article, and its output was worked out by following each statement in order. Notice that 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. Reading the trace line by line is how you find the statement responsible for any surprising value.

python
Output (expected)
15
12
24
6.0
2.0
Notes on the code
  • x = 10 stores the starting value; nothing is printed yet
  • x += 5 reads 10, adds 5, and stores 15; the first print shows 15
  • x -= 3 reads 15, subtracts 3, and stores 12; the second print shows 12
  • x *= 2 reads 12, multiplies by 2, and stores 24; the third print shows 24
  • x /= 4 reads 24, divides by 4, and stores 6.0; the fourth print shows 6.0
  • x %= 4 reads 6.0, takes the remainder after dividing by 4, and stores 2.0; the fifth print shows 2.0
10x = 10 (start)15after x += 512after x -= 324after x *= 26.0after x /= 42.0after x %= 4+= 5-= 3*= 2/= 4%= 4
After each compound assignment runs in order, what value does x hold, and which printed line does that statement produce?

Read the diagram from left to right. Each arrow is one compound assignment, and the value at the arrowhead is what x holds once that statement has run. The printed lines in the code are the same five values, in the same order, after the four updates that change them and the starting value that is never printed. Matching each line this way is the reliable method when a printed value looks wrong.

Reading x op= y as x = x op y

A compound assignment combines an operator with the equals sign, so one statement both computes a new value and stores it back into the same variable. The source lists the forms x+=y, x-=y, x*=y, x/=y, and x%=y, along with the increment and decrement forms ++x and --x. The source defines x+=y as x = x+y, and the other forms follow the same pattern with their own operators.

Key point

To read any compound statement, rewrite it as x = x op y. The variable on the left is updated from its current value, and the result replaces the old value in that same variable.

Before
x = 10
x += 5

x holds 10 before the statement runs

After
x = 10
x = x + 5

x holds 15; the longer form gives the same result

Apply the rewrite to the other statements from the trace. x -= 3 means x = x - 3, so it reads the current value 15, subtracts 3, and stores 12. x *= 2 means x = x * 2, and x %= 4 means x = x % 4. In every case the right-hand side reads the value x holds at that moment, which is why the same statement gives different results at different points in a program.

Worked example

Rewriting a Compound Assignment in Longer Form

Given total = 40 followed by total -= 15, what value does total hold afterward, and what is the longer form of the second statement?

  1. 1
    Identify the target, operator, and operand

    In total -= 15, the target is total, the operator is minus combined with the equals sign, and the operand is 15.

  2. 2
    Write the longer form

    Replace the compound operator with x = x op y, giving total = total - 15.

  3. 3
    Read the current value on the right

    The right side reads total as it is before the statement, which is 40.

  4. 4
    Compute and store the result

    40 minus 15 is 25, and that 25 is stored back into total.

Result

total = total - 15 is the longer form, and total holds 25 afterward.

Anatomy of a Compound Assignment

Read the most common form, x+=y, 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 labels each part so you can point to what each piece contributes.

totaltarget: the variable beingchanged-=operator: minus combinedwith equalsdiscountoperand: the value used forthe change
In a statement like total -= discount, which part is the variable being changed, which is the operator, and which is the value used to change it?
x = 7x holds 7 before thestatementx += 3compound formx = x + 3longer form, reads 7 thenadds 3x = 10stored back into xwritten asequivalent toresult 10 stored
When x += 3 runs, which value of x is read on the right side, what is computed, and where is the result stored?

Mistakes When Reading Compound Operators

Most errors when reading these operators come from treating the statement as a single word rather than as a rewrite. The following mistakes are the ones to check for when a printed value does not match what you expected.

  • Common mistake

    Forgetting that the right side reads the variable's current value

    What it looks like

    With x = 10, the statement x += 5 is read as x = 5 + something fixed, instead of x = x + 5.

    Why it fails

    The left side is updated from its current value, so the result depends on what x held just before the statement ran.

    Do this instead

    Rewrite the statement as x = x op y and substitute the value x holds at that moment: x = 10 + 5 gives 15.

  • Common mistake

    Reading x -= y as y minus x

    What it looks like

    With x = 15 and y = 3, reading x -= 3 as 3 minus 15 gives a negative result.

    Why it fails

    The variable on the left is the first operand. The longer form x = x - y puts x first, so the result is x minus y.

    Do this instead
    Read x -= 3 as x = x - 3, giving 12 from 15.
  • Common mistake

    Reading x /= y as y divided by x

    What it looks like

    With x = 24 and y = 4, reading x /= 4 as 4 divided by 24 gives a fraction close to 0.17.

    Why it fails

    The same operand order applies to every operator. The current value of x is the left operand.

    Do this instead
    Read x /= 4 as x = x / 4, giving 6.0 from 24.
  • Common mistake

    Expecting the printed line to come from a different statement

    What it looks like

    Seeing 24 printed and assuming it came from the division.

    Why it fails

    Each printed line is produced by the statement immediately before it in the program order.

    Do this instead

    Match each printed line to its statement in order, as in the trace: 24 came from x *= 2.

Practicing the Read-Rewrite-Trace Routine

EASY

Given y = 8, write the statements y *= 3 and y -= 5 in their longer form, then trace the value of y after each one and state which printed line each value would produce.

Hints
  • The longer form of y *= 3 is y = y * 3.
  • Start with y holding 8, then use the value it holds after the first statement for the second one.
  • After y *= 3, y holds 24; after y -= 5, y holds 19.
MEDIUM

Write a short program that starts with count = 0, applies count += 1 three times, then applies count *= 5, printing count after each statement. Predict the four printed lines before you run it.

Hints
  • Each += 1 adds one to the value stored from the previous line.
  • The expected lines are 1, 2, 3, then 15.

Key Takeaways

  • A compound assignment such as x+=y modifies a variable in one statement, and the source defines it as x = x+y.
  • To read any compound statement, rewrite it as x = x op y, reading the current value of x on the right.
  • Trace a run by matching each printed line to the statement that produced it, in program order.
  • The variable on the left is always the first operand, so x -= y means x minus y and x /= y means x divided by y.

At a glance

  • A compound assignment such as x+=y modifies a variable in one statement, and the source defines it as x = x+y.
  • Rewrite any compound statement as x = x op y to see which current value is read and where the result is stored.
  • Trace a sequence of compound assignments by matching each printed line to the statement that produced it.
  • Read the variable on the left as the first operand, so x -= y means x minus y, not y minus x.

Related concepts

More concepts starting with I