# 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 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!

Coding Exercise

Run the code below to see what the ++ and shorcut operators do. Use the Codelens 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.

## 1.5.1. 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.2. Prefix versus Postfix Operator¶

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. Click on CodeLens to step through the execution. Notice that the second println prints the original value 7 even though the memory location for variable `count` 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

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--;
```