Compound assignment operators +=, -=, *=, /=, and %= 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 way to remember the order of the two characters in the compound operators, think about the order things happen: first we use the math operator, such as +, to get the new value and then the value is assigned back to the variable with =. So itβs operator then equals 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. 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.
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 increment 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!
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.
If the operator is after the variable name (called the postfix operator), the value of the variable is changed after evaluating the variable to get its value. And if the operator is 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. For example, 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.
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.
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. Create more compound assignment statements with shortcut operators as described below and work with a partner to guess what they would print out before running the code.
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.
(AP 1.6.A.1) Compound assignment operators (+=, -=, *=, /=, %=) can be used in place of the assignment operator in numeric expressions. A compound assignment operator performs the indicated arithmetic operation between the current value of the variable on the left and the value on the right and then assigns the resulting value to the variable on the left.
(AP 1.6.A.2) The increment operator (++) and decrement operator (--) are used to add 1 or subtract 1 from the stored value of a numeric variable. The new value is assigned to the variable.