Assignment Statements
++ increments a variable by 1, and -- decrements a variable by 1.
A Score That Has to Keep Changing
Imagine a program tracking how many goals a team has scored. Each time a goal goes in, the stored count has to rise by one. Programs handle this kind of change with assignment statements, the basic mechanism for storing values in variables. In this article you will follow that mechanism step by step, from a statement that stores a computed result to the timing rules of the increment and decrement operators, which cause many bugs when they appear inside larger expressions.
What do you think happens?
If x holds 5 and the statement y = x++ runs, what value does y receive?
Reveal answer
5
x++ is post-increment. It produces the old value of x, which is 5, so y receives 5. Only after that is x increased by 1, leaving x at 6. The section on old and new values traces this in detail.
Storing a Value with Assignment
An assignment statement tells the computer to store a value in a variable.
The general form is variable = expression; The left side names the destination that receives the value. The right side is the expression, which describes the computation that produces the value. In an assignment, the variable is the part receiving the value, and the expression is the part being computed.
- interest is the destination variable on the left
- principal * 0.07 is the expression on the right
- principal is read in the computation and is not the destination
This statement uses the value already held in principal, multiplies it by 0.07, and stores the result in interest. The assignment therefore combines two actions: computing with existing data and storing the result of that computation. Assignment statements connect computation with storage.
Read the diagram from left to right. The computation happens in the middle, and only at the end does a value land in the variable on the far right. When you trace any assignment, follow the same path: first find what the expression computes, then find the variable that receives it.
Increment and Decrement Operators
The increment operator ++ increases a variable by 1, and the decrement operator -- decreases a variable by 1. Whatever form you use, the operator always changes the variable by 1. The question that matters is which value the surrounding expression produces, and that is covered in the next section.
A common mistake is to treat x++ as if it were simply x + 1. They are not the same. The expression x + 1 computes a value but does not change x. The expression x++ changes x by adding 1, and it also produces a value that can be used in a larger expression. This difference is the source of many bugs.
The decrement forms follow the same pattern. The rule you learn for ++ carries over to --, so once you understand the timing for x++ you can apply the same reasoning to x--.
Old Value Versus New Value
The position of ++ determines which value the expression produces. This is the central idea of this article. The operator does its change to the variable either way, but the value handed to the rest of the expression depends on where the operator sits.
With post-increment, written x++, the old value of x is produced first, and then x is increased by 1. The diagram shows that order: the value 5 is produced and reaches y before x becomes 6.
With pre-increment, written ++x, x is increased by 1 first, and the new value is produced. So the same starting value of 5 gives a different result for y in that case, because the increase happens before the value is read.
Post-increment inside an assignment
x holds 5. The statement y = x++; runs. What do x and y hold afterward?
- 1Identify the old value
x++ produces the old value of x, which is 5, before x changes.
- 2Assign the produced value
The assignment stores the produced value 5 in y.
- 3Account for the change to x
x is then increased by 1, so it now holds 6. Tracing only the assignment would miss this change.
y holds 5 and x holds 6.
Pre-increment inside an assignment
x holds 5. The statement y = ++x; runs. What do x and y hold afterward?
- 1Increase x first
The pre-increment changes x before anything else happens, so x becomes 6.
- 2Produce the new value
The expression now produces the new value of x, which is 6.
- 3Assign the produced value
The assignment stores 6 in y.
y holds 6 and x holds 6.
The same timing applies to decrement. In y = x--, y receives the old value of x, and then x is reduced by 1. In y = --x, x is reduced by 1 first, and y receives the new value.
Why x = x++ Leaves x Unchanged
The statement x = x++ looks as if it should increase x, but it does not change the final value of x. The reason follows from the timing rules above. The post-increment first produces the old value of x, which is used for the assignment. Although x is incremented during the operation, the assignment then stores that old value back into x. The final value of x is therefore unchanged.
The state diagram follows x through the statement. x begins at 5, rises to 6 during the increment, and then receives the old value 5 from the assignment. The temporary rise to 6 is real, but it does not survive the statement.
x = 5;x holds 5 before the statement runs
x = x++;x still holds 5 after the statement runs
Standalone Increment Statements
Increment and decrement operators are easiest to understand when they are used as standalone statements. A statement such as counter++ clearly commands the program to increase counter, and goalsScored++ clearly increases goalsScored. Here the operator is used for its change to the variable, not for a value inside a larger expression.
- each statement changes its variable by 1
- no value is taken from either expression, so the timing question does not arise
- Does the operator change the variable? A ++ or -- always changes the variable by 1.
- Which value does the expression produce? A post-operator produces the old value, and a pre-operator produces the new value.
- If no surrounding expression needs the produced value, use the operator as a standalone statement to keep the intent clear.
Mistakes with Increment and Assignment
Common mistake Treating x++ as if it were just x + 1
What it looks likeWith x holding 5, y = x + 1; leaves x at 5, while y = x++; leaves x at 6 and gives y the value 5.
Why it failsx + 1 computes a value without changing x. x++ changes x and also produces a value.
Do this insteadAsk whether the operator changes the variable, and trace that change separately from the produced value.
Common mistake Assuming a post-increment expression produces the new value
What it looks likeWith x holding 5, assuming y = x++; gives y the value 6.
Why it failsA post-operator produces the old value of x, so y receives 5.
Do this insteadFor post-increment, identify the old value first, then account for the change to x.
Common mistake Tracking only the assignment and missing the change to x
What it looks likeAfter y = x++; with x holding 5, a learner who checks only y may conclude that x stayed at 5.
Why it failsThe increment still happens during the operation, so x ends at 6.
Do this insteadTrace two separate results: the value the expression produces and the new value stored in x.
Common mistake Expecting x = x++ to increase x
What it looks likeWith x holding 5, x = x++; leaves x at 5.Why it failsThe old value is produced and assigned back into x after the increment.
Do this insteadRemember that the final assignment writes the old value, so x is unchanged.
Tracing Assignments in Practice
A variable x starts at 3. Trace these two statements in order: y = ++x; then z = x--; Give the final values of x, y, and z.
Hints
- For y = ++x, first change x, then take the new value for y.
- For z = x--, z receives the value of x before the decrement, and then x is reduced by 1.
- Track x after each statement, not only the value assigned in each line.
Key Takeaways
- An assignment statement stores a value in a variable: the variable on the left receives the result of the expression on the right.
- The increment operator ++ adds 1 to a variable, and the decrement operator -- subtracts 1; either operator always changes the variable.
- Post-increment and post-decrement produce the old value, while pre-increment and pre-decrement change the variable first and produce the new value.
- x = x++ leaves x unchanged because the old value produced by the post-increment is assigned back into x, and tracing the produced value and the change to x separately prevents this and similar errors.