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.
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.
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.
a *= 3: This line multiplies a by 3 and assigns the result back to a. The value of a is now 18.
b -= 2: This line subtracts 2 from b and assigns the result back to b. The value of b is now 2.
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.
a += c: This line adds c to a and assigns the result back to a. The value of a is now 18.
b = a - b: This line subtracts b from a and assigns the result back to b. The value of b is now 16.
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:
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:
x /= y: This line divides x by y and assigns the result back to x. The value of x is now 3.75.
y *= x: This line multiplies y by x and assigns the result back to y. The value of y is now 15.0.
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.
x += z: This line adds z to x and assigns the result back to x. The value of x is now 7.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.
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:
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:
a -= b: This line subtracts b from a and assigns the result back to a. The value of a is now 50.
b *= 2: This line multiplies b by 2 and assigns the result back to b. The value of b is now 100.
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.
a = b + c: This line adds b and c and assigns the result to a. The value of a is now 101.
b = c - a: This line subtracts a from c and assigns the result to b. The value of b is now -100.
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:
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:
a *= 2: This line multiplies a by 2 and assigns the result back to a. The value of a is now 10.
b -= 1: This line subtracts 1 from b and assigns the result back to b. The value of b is now 2.
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.
a += c: This line adds c to a and assigns the result back to a. The value of a is now 10.
b = a - b: This line subtracts b from a and assigns the result back to b. The value of b is now 8.
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:
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:
x *= 2: This line multiplies x by 2 and assigns the result back to x. The value of x is now 10.
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).
z -= x: This line subtracts x from z and assigns the result back to z. The value of z is now 5.
x = y + z: This line adds y and z and assigns the result to x. The value of x is now 8.
y = z - x: This line subtracts x from z and assigns the result to y. The value of y is now -3.
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:
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:
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).
y *= x: This line multiplies y by x and assigns the result back to y. The value of y is now 10.
z = y - x: This line subtracts x from y and assigns the result to z. The value of z is now 6.67.
x += z: This line adds z to x and assigns the result back to x. The value of x is now 10.0.
y = x / z: This line divides x by z and assigns the result back to y. The value of y is now 1.5.
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:
Want some additional practice? CSAwesome created this really cool
Operators Maze game that you can do with a friend for a little extra practice!Β