Java TOPIC
07 – ARITHMETIC EXPRESSIONS AP EXTRA AP LESSON NOTE MORE ON ++ OPERATOR We've seen that ++ is an operator that
can be used to increment a variable.
Similarly, the – operator is used with a variable to decrement it. However, we can also actually use them
as part of an arithmetic expression.
When this is done, the incrementing or decrementing occurs only after
the value in the variable is used for evaluation of the arithmetic expression.
Let's consider the following two lines of
code: double b = c++ * 3; After line 1, c gets the value 2. To do the arithmetic expression on line 2,
Java gets the value from c, which is 2 and then increments c. But the expression is evaluated to 6 still
as Java uses the value that c had before being incremented. In the end, b gets 6 and c gets 3. EXAMPLE 2 Let's consider the following three lines of
code: int c = 1; int d = c++ * 3 + c; System.out.println(d); Note that this is getting a little
strange. But it is important to know
how it works. The challenge is on line 2. What will d get? We start by doing the multiplication
which gives us 1 * 3 which is 3. As
well, c gets incremented at this point to the value 2. Now we add c, which is now 2, to get a
total of 5.
Let's consider the following three lines of
code: c
= c++ * 2; System.out.println(c); Again, this is a strange example. I would discourage such use of code. But let's examine. The challenge here is figuring out
when the incrementing on c occurs. Here's
how it works: We know that the expression on the
right hand side of line 2 gets evaluated to 10. Then c is incremented to 6. Then, the value 10 is assigned to the
variable on the left hand side of the equals, which happens to be c. So c actually gets the value 10 and the
incrementing doesn't really do anything here. EXAMPLE 4 There is a problem that arrises. BEDMAS
applies for the order of operations.
However, before doing the operations, Java recopies the expression
with all actual values instead of variables.
And this process happens from left to right. This has an important concequence
which is evidence that we should never use two ++ operators on the same line. The consequence is that these two
programs give a different result:
At first, looking at the expression,
we could easily conclude that they are exactly the same. However, the order in which the c is
incremented matters. And it is from
left to right. Here is how the expression is
rewritten by Java:
Note that the above is an extreme
example and programmers should avoid using such statements. THE
+= OPERATOR The statement a += 5; is simply a short form for a = a + 5; So, the += operator adds the value to
the right of it to the variable in question. THE
-= OPERATOR Similarly to the += operator, the -=
operator simply reduces the variable in question by the amount to the right. So, b -= 2; is the same as b = b - 2; THE
*= OPERATOR More of the same. The *= operator will multiply the variable
in question by the value on the right. THE
/= OPERATOR This operator is the same but for
division. THE
%= OPERATOR Ok.
This gets strange. But it works
in Java. And it’s the same as the
other similar operators. The statement a %= 30; is equivalent to a = a % 30; |
||||
|