1.5. Compound Assignment Operators¶
Compound assignment operators are shortcuts that do a math operation and
assignment in one step. For example, x += 1
adds 1 to the current value of
x
and assigns the result back to x
. It is the same as x = x + 1
.
This pattern is possible with any operator put in front of the =
sign, as
seen below. If you need a mnemonic to remember whether the compound operators
are written like +=
or =+
, just remember that the operation (+
) is
done first to produce the new value which is then assigned (=
) back to the
variable. So it’s operator then equal sign: +=
.
Since changing the value of a variable by one is especially common, there are
two extra concise operators ++
and --
, also called the plus-plus or
increment operator and minus-minus or decrement operator that set a
variable to one greater or less than its current value.
Thus x++
is even more concise way to write x = x + 1
than the compound
operator x += 1
. You’ll see this shortcut used a lot in loops when we get to
them in Unit 4. Similarly, y--
is a more concise way to write y = y - 1
.
These shortcuts only exist for +
and -
as they don’t really make sense
for other operators.
Note
If you’ve heard of the programming language C++, the name is an inside joke that C, an earlier language which C++ is based on, had been incremented or improved to create C++.
Here’s a table of all the compound arithmetic operators and the extra concise incremend and decrement operators and how they relate to fully written out assignment expressions. You can run the code below the table to see these shortcut operators in action!
Operator |
|
|
|
|
|
---|---|---|---|---|---|
Written out |
|
|
|
|
|
Compound |
|
|
|
|
|
Extra concise |
|
|
|||
Run the code below to see what the ++ and shorcut operators do. Click on the Show Code Lens button to trace through the code and the variable values change in the visualizer. Try creating more compound assignment statements with shortcut operators and work with a partner to guess what they would print out before running the code.
Note
If you look at real-world Java code, you may occassionally see the ++
and
--
operators used before the name of the variable, like ++x
rather
than x++
. That is legal but not something that you will see on the AP
exam.
Putting the operator before or after the variable only changes the value of
the expression itself. If x
is 10 and we write,
System.out.println(x++)
it will print 10 but aftewards x
will be 11.
On the other hand if we write, System.out.println(++x)
, it will print 11
and afterwards the value will be 11.
In other words, with the operator after the variable name, (called the postfix operator) the value of the variable is changed after evaluating the variable to get its value. And with the operator before the variable (the prefix operator) the value of the variable in incremented before the variable is evaluated to get the value of the expression.
But the value of x
after the expression is evaluated is the same in
either case: one greater than what it was before. The --
operator works
similarly.
The AP exam will never use the prefix form of these operators nor will it use the postfix operators in a context where the value of the expression matters.
- x = -1, y = 1, z = 4
- This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
- x = -1, y = 2, z = 3
- This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
- x = -1, y = 2, z = 2
- This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
- x = 0, y = 1, z = 2
- This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
- x = -1, y = 2, z = 4
- This code subtracts one from x, adds one to y, and then sets z to to the value in z plus the current value of y.
1-5-2: What are the values of x, y, and z after the following code executes?
int x = 0;
int y = 1;
int z = 2;
x--;
y++;
z+=y;
- x = 6, y = 2.5, z = 2
- This code sets x to z * 2 (4), y to y divided by 2 (5 / 2 = 2) and z = to z + 1 (2 + 1 = 3).
- x = 4, y = 2.5, z = 2
- This code sets x to z * 2 (4), y to y divided by 2 (5 / 2 = 2) and z = to z + 1 (2 + 1 = 3).
- x = 6, y = 2, z = 3
- This code sets x to z * 2 (4), y to y divided by 2 (5 / 2 = 2) and z = to z + 1 (2 + 1 = 3).
- x = 4, y = 2.5, z = 3
- This code sets x to z * 2 (4), y to y divided by 2 (5 / 2 = 2) and z = to z + 1 (2 + 1 = 3).
- x = 4, y = 2, z = 3
- This code sets x to z * 2 (4), y to y divided by 2 (5 / 2 = 2) and z = to z + 1 (2 + 1 = 3).
1-5-3: What are the values of x, y, and z after the following code executes?
int x = 3;
int y = 5;
int z = 2;
x = z * 2;
y = y / 2;
z++;
1.5.1. Code Tracing Challenge and Operators Maze¶
Use paper and pencil or the question response area below to trace through the following program to determine the values of the variables at the end.
Code Tracing is a technique used to simulate a dry run through the code or pseudocode line by line by hand as if you are the computer executing the code. Tracing can be used for debugging or proving that your program runs correctly or for figuring out what the code actually does.
Trace tables can be used to track the values of variables as they change throughout a program. To trace through code, write down a variable in each column or row in a table and keep track of its value throughout the program. Some trace tables also keep track of the output and the line number you are currently tracing.
or
Trace through the following code:
int x = 0;
int y = 5;
int z = 1;
x++;
y -= 3;
z = x + z;
x = y * z;
y %= 2;
z--;
1-5-4: Write your trace table for x, y, and z here showing their results after each line of code.
After doing this challenge, play the Operators Maze game. See if you and your partner can get the highest score!
1.5.2. Summary¶
Compound assignment operators (
+=
,-=
,*=
,/=
,%=
) can be used in place of the assignment operator.The increment operator (
++
) and decrement operator (--
) are used to add 1 or subtract 1 from the stored value of a variable. The new value is assigned to the variable.The use of increment and decrement operators in prefix form (e.g.,
++x
) and inside other expressions (i.e.,arr[x++]
) is outside the scope of this course and the AP Exam.