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. You should know that x += y is the same as x = x + y, x -= y is the same as x = x - y, x *= y is the same as x = x * y, and x /= y is the same as x = x / y.

The most common shortcut ++, the plus-plus or increment operator, is used to add one 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. You may have also seen it in the language C++ which is an inside joke that C has been incremented or improved to create C++. The -- operator is used to subtract one 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 ++ in action!

Note

On the exam you can use x++ or ++x to both 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; or int y = x++;. In int y = ++x; the value of x would be incremented before y’s value is set to a copy of x’s value. In int y = x++; the value of y would be set to a copy of x’s value before x is incremented. The exam will never use a shortcut in an assignment statement, so you don’t need to worry about the difference between ++x or x++.

exercise Check Your Understanding

    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--; // x followed by the double minus sign
    y++;
    z+=y;
    
  • 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-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 = y / 2;
    z++;
    
  • 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.1. groupwork Programming Challenge

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 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.

x

y

z

Output

int x = 0;
int y = 5;
int z = 1;
x++;
y -= 3;
z = x + z;
x = y * z;
y %= 2;
z--;

1-5-3: Write your trace table for x, y, and z here showing their results after each line of code.

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.

You have attempted of activities on this page
Next Section - 1.6. Casting and Ranges of Variables