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

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

On the exam you can use `x++`

or `++x`

to add one to the value of `x`

. These two shortcuts only have different results if you assign the value of `x`

to another variable as in `int y = ++x;`

(where the value of x is incremented before y is set to it) or `int y = x++;`

(where y is set to a copy of x’s value before x is incremented). The AP exam will never use a shortcut in an assignment statement, so you don’t need to worry about the difference between `++x`

or `x++`

.

- 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
- x = -1, y = 2, z = 4

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 (i.e., ++x) and inside other expressions (i.e., arr[x++]) is outside the scope of this course and the AP Exam.