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 x and assigns the sum 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.
+ shortcuts |
- shortcuts |
* shortcut |
/ shortcut |
% shortcut |
---|---|---|---|---|
x = x + 1; |
x = x - 1; |
x = x * 2; |
x = x / 2; |
x = x % 2; |
x += 1; |
x -= 1; |
x *= 2; |
x /= 2; |
x %= 2; |
x++; |
x- -; |
The most common shortcut operator ++
, the plus-plus or increment operator, is used to add 1 to the current value; x++
is the same as x += 1
and the same as x = x + 1
. It is a shortcut that is used a lot in loops. If you’ve heard of the programming language C++, the ++ in C++ is an inside joke that C has been incremented or improved to create C++. The --
decrement operator is used to subtract 1 from the current value: y--
is the same as y = y - 1
. These are the only two double operators; this shortcut pattern does not exist with other operators. Run the following code to see these shortcut operators in action!
Run the code in
E01ShortcutOperators
to see what the ++ and shorcut operators do. Use the debugger to trace through the code and observe how the variable values change. Try creating more compound assignment statements with shortcut operators and guess what they would print out before running the code.
- 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 = -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 = -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-1: 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-2: 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 /= 2;
z++;
Code Tracing Challenge and Operators Maze¶
Code Tracing is a technique used to simulate by hand a dry run through the code or pseudocode 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.
For example, given the following code:
x = 10;
y = 15;
z = x * y;
z++;
x = z * 2;
The corresponding trace table looks like this:
Line |
Statement |
x |
y |
z |
---|---|---|---|---|
1 |
x = 10; |
10 |
||
2 |
y = 15; |
15 |
||
3 |
z = x * y; |
150 |
||
4 |
z++; |
151 |
||
5 |
x = z * 2; |
302 |
Alternatively, we can show a compressed trace by listing the sequence of values assigned to each variable as the program executes. You might want to cross off the previous value when you assign a new value to a variable. The last value listed is the variable’s final value.
Use paper and pencil to trace through the following program to determine the
values of the variables at the end. Be careful, %
is the remainder operator, not division.
1-5-3:
int x = 0;
int y = 5;
int z = 1;
x++;
y -= 3;
z = x + z;
x = y * z;
y %= 2;
z--;
The final value for x is
The final value for y is
The final value for z is
Prefix versus Postfix Operator¶
Open the
E02PostfixExample
program. What do you think is printed when the following code is executed? Try to guess the output before running the code. You might be surprised at the result. Use the debugger to step through the execution. Notice that the second println prints the original value 7 even though the memory location for variablecount
is updated to the value 8.
The code System.out.println(count++)
adds one to the variable after the value is printed.
Try changing the code to ++count
and run it again. This will result in one being added to
the variable before its value is printed.
When the ++
operator is placed before the variable, it is called prefix increment.
When it is placed after, it is called postfix increment.
Example |
Description |
Type |
---|---|---|
System.out.println(count++); |
Print the current value of count, then add one to count |
Postfix |
System.out.println(++count); |
Add one to count, then print the new value |
Prefix |
x = y++; |
Copy the value of y into x, then add one to y |
Postfix |
x = ++y; |
Add one to y, then copy the value of y into x |
Prefix |
x = y- -; |
Copy the value of y into x, then subtract one from y |
Postfix |
x = - -y; |
Subtract one from y, then copy the value of y into x |
Prefix |
-
1-5-4: Assume score=5 and each line of code is executed independent of the others. Match each line of code to the correct result (drag the code block to the matching result).
Try again.
- System.out.println(score++);
- Print the value 5, then assign score the value 6.
- System.out.println(score--);
- Print the value 5, then assign score the value 4.
- System.out.println(++score);
- Assign score the value 6, then print the value 6.
- System.out.println(--score);
- Assign score the value 4, then print the value 4.
Note
When you are new to programming, it is advisable to avoid mixing unary operators ++
and --
with assignment or
print statements. Try to perform the increment or decrement operation on a separate line of code
from assignment or printing.
For example, instead of writing x=y++;
or System.out.println(z--);
the code below makes it clear that the increment of y happens after the
assignment to x, and that the value of z is printed before it is decremented.
x=y;
y++;
System.out.println(z);
z--;
-
1-5-5: Match each single line of code on the left to the equivalent pair of lines on the right.
Try again.
- System.out.println(score++);
- System.out.println(score); score++;
- System.out.println(score--);
- System.out.println(score); score--;
- System.out.println(++score);
- score++; System.out.println(score);
- System.out.println(--score);
- score--; System.out.println(score);
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.