<< Hide Menu
7 min readโขjune 18, 2024
Avanish Gupta
user_sophia9212
Avanish Gupta
user_sophia9212
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)
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.
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:
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:
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:
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:
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:
Want some additional practice? CSAwesome created this really coolย Operators Maze game that you can do with a friend for a little extra practice!ย
ยฉ 2024 Fiveable Inc. All rights reserved.