Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
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 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 mnemonic to remember whether the compound operators
are written like
=+, just remember that the operation (
done first to produce the new value which is then assigned (
=) back to the
variable. So it’s operator then equal sign:
Since changing the value of a variable by one is especially common, there are
two extra concise operators
--, 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.
x++ is even more concise way to write
x = x + 1 than the compound
x += 1. You’ll see this shortcut used a lot in loops when we get to
them in Unit 4. Similarly,
y-- is a more concise way to write
y = y - 1.
These shortcuts only exist for
- 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 incremend 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!
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.
If you look at real-world Java code, you may occassionally see the
-- operators used before the name of the variable, like
x++. That is legal but not something that you will see on the AP
Putting the operator before or after the variable only changes the value of
the expression itself. 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.
In other words, with the operator after the variable name, (called the postfix operator) the value of the variable is changed after evaluating the variable to get its value. And with the operator 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.
But the value of
x after the expression is evaluated is the same in
either case: one greater than what it was before. The
-- operator works
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.
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.
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!
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 (e.g.,
++x) and inside other expressions (i.e.,
arr[x++]) is outside the scope of this course and the AP Exam.