πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

βž•

1.4 Compound Assignment Operators

7 min readβ€’december 27, 2022

Athena_Codes

Athena_Codes

user_sophia9212

user_sophia9212


AP Computer Science AΒ πŸ’»

130Β resources
See Units

Compound Assignment Operators

Compound Operators

Sometimes, you will encounter situations where you need to perform the following operation:
int integerOne = 6; integerOne = integerOne * 2;
This is a bit clunky with the repetition of integerOne in line two. We can condense this with this statement:
integerOne *= 2;
The "*= 2" is an example of a compound assignment operator, which multiplies the current value of integerOne by 2 and sets that as the new value of integerOne. Other arithmetic operators also have compound assignment operators as well, with addition, subtraction, division, and modulo having +=, -=, /=, and %=, respectively.

Incrementing and Decrementing

There are special operators for the two following operations in the following snippet well:
integerOne += 1; integerTwo -= 1;
These can be replaced with a pre-increment/pre-decrement (++i or - -i) or post-increment/post-decrement (i++ or i- -) operator. You only need to know the post-variant in this course, but it is useful to know the difference between the two. Here is an example demonstrating the difference between them:
int integerOne = 2; integerOne++; System.out.println(integerOne); ++integerOne; System.out.println(integerOne); System.out.println(integerOne++); System.out.println(++integerOne); 3 4 4 6
By itself, there is no difference between the pre-increment and post-increment operators, but it's evident when you use it in a method such as the println method. For this statement, I will write a debugging output, which happens when we trace the code, which means to follow it line-by-line.
Value of integerOne after line 1: 2 Value of integerOne after line 2: 3 Value of integerOne after line 3: 3 Value of integerOne after line 4: 4 Value of integerOne after line 5: 4 Value of integerOne before printing on line 6: 4 Value of integerOne after line 6: 5 (post-increment increments after the method) Value of integerOne before printing on line 7: 6 Value of integerOne after line 7: 6 (pre-increment increments before the method)
Code Tracing Practice
Now that you’ve learned about code tracing, let’s do some practice! You can use trace tables like the ones shown below to keep track of the values of your variables as they change.
x
y
z
output
x
y
z
output
Here are some practice problems that you can use to practice code tracing. Feel free to use whichever method you’re the most comfortable with!
Trace through the following code:
int a = 6;
int b = 4;
int c = 0;
a *= 3;
b -= 2;
c = a % b;
a += c;
b = a - b;
c *= b;
Answer:
Note: Your answers could look different depending on how you’re tracking your code tracing.
  1. a *= 3: This line multiplies a by 3 and assigns the result back to a. The value of a is now 18.
  2. b -= 2: This line subtracts 2 from b and assigns the result back to b. The value of b is now 2.
  3. c = a % b: This line calculates the remainder of a divided by b and assigns the result to c. The value of c is now 0.
  4. a += c: This line adds c to a and assigns the result back to a. The value of a is now 18.
  5. b = a - b: This line subtracts b from a and assigns the result back to b. The value of b is now 16.
  6. c *= b: This line multiplies c by b and assigns the result back to c. The value of c is now 0.
The final values of the variables are:
  • a: 18
  • b: 16
  • c: 0
Trace through the following code:
double x = 15.0;
double y = 4.0;
double z = 0;
x /= y;
y *= x;
z = y % x;
x += z;
y = x / z;
z *= y;
Answer:
  1. x /= y: This line divides x by y and assigns the result back to x. The value of x is now 3.75.
  2. y *= x: This line multiplies y by x and assigns the result back to y. The value of y is now 15.0.
  3. z = y % x: This line calculates the remainder of y divided by x and assigns the result to z. The value of z is now 3.75.
  4. x += z: This line adds z to x and assigns the result back to x. The value of x is now 7.5.
  5. y = x / z: This line divides x by z and assigns the result back to y. The value of y is now 2.0.
  6. z *= y: This line multiplies z by y and assigns the result back to z. The value of z is now 7.5.
The final values of the variables are:
  • x: 7.5
  • y: 2.0
  • z: 7.5
Trace through the following code:
int a = 100;
int b = 50;
int c = 25;
a -= b;
b *= 2;
c %= 4;
a = b + c;
b = c - a;
c = a * b;
Answer:
  1. a -= b: This line subtracts b from a and assigns the result back to a. The value of a is now 50.
  2. b *= 2: This line multiplies b by 2 and assigns the result back to b. The value of b is now 100.
  3. c %= 4: This line calculates the remainder of c divided by 4 and assigns the result back to c. The value of c is now 1.
  4. a = b + c: This line adds b and c and assigns the result to a. The value of a is now 101.
  5. b = c - a: This line subtracts a from c and assigns the result to b. The value of b is now -100.
  6. c = a * b: This line multiplies a and b and assigns the result to c. The value of c is now -10201.
The final values of the variables are:
  • a: 101
  • b: -100
  • c: -10201
Trace through the following code:
int a = 5;
int b = 3;
int c = 0;
a *= 2;
b -= 1;
c = a % b;
a += c;
b = a - b;
c *= b;
Answer:
  1. a *= 2: This line multiplies a by 2 and assigns the result back to a. The value of a is now 10.
  2. b -= 1: This line subtracts 1 from b and assigns the result back to b. The value of b is now 2.
  3. c = a % b: This line calculates the remainder of a divided by b and assigns the result to c. The value of c is now 0.
  4. a += c: This line adds c to a and assigns the result back to a. The value of a is now 10.
  5. b = a - b: This line subtracts b from a and assigns the result back to b. The value of b is now 8.
  6. c *= b: This line multiplies c by b and assigns the result back to c. The value of c is now 0.
The final values of the variables are:
  • a: 10
  • b: 8
  • c: 0
Trace through the following code:
int x = 5;
int y = 10;
int z = 15;
x *= 2;
y /= 3;
z -= x;
x = y + z;
y = z - x;
z = x * y;
Answer:
  1. x *= 2: This line multiplies x by 2 and assigns the result back to x. The value of x is now 10.
  2. y /= 3: This line divides y by 3 and assigns the result back to y. The value of y is now 3.3333... (rounded down to 3).
  3. z -= x: This line subtracts x from z and assigns the result back to z. The value of z is now 5.
  4. x = y + z: This line adds y and z and assigns the result to x. The value of x is now 8.
  5. y = z - x: This line subtracts x from z and assigns the result to y. The value of y is now -3.
  6. z = x * y: This line multiplies x and y and assigns the result to z. The value of z is now -24.
The final values of the variables are:
  • x: 8
  • y: -3
  • z: -24
Trace through the following code:
double x = 10;
double y = 3;
double z = 0;
x /= y;
y *= x;
z = y - x;
x += z;
y = x / z;
z *= y;
Answer:
  1. x /= y: This line divides x by y and assigns the result back to x. The value of x is now 3.3333... (rounded down to 3.33).
  2. y *= x: This line multiplies y by x and assigns the result back to y. The value of y is now 10.
  3. z = y - x: This line subtracts x from y and assigns the result to z. The value of z is now 6.67.
  4. x += z: This line adds z to x and assigns the result back to x. The value of x is now 10.0.
  5. y = x / z: This line divides x by z and assigns the result back to y. The value of y is now 1.5.
  6. z *= y: This line multiplies z by y and assigns the result back to z. The value of z is now 10.0.
The final values of the variables are:
  • x: 10.0
  • y: 1.5
  • z: 10.0
Want some additional practice? CSAwesome created this really cool Operators Maze game that you can do with a friend for a little extra practice!Β 
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.