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.


EXAMPLE

 Let's consider the following two lines of code:

       double c = 2;

       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.

So in the situation above, the incrementing actually affected the evaluated result because the variable c is used twice.


EXAMPLE 3

 Let's consider the following three lines of code:

       int c = 5;

       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:

 

int c = 5;

int d = c++ + c++ * 2;

System.out.println(d);

 

//Gives 17

int c = 5;

int d = c++ * 2 + c++;

System.out.println(d);

 

//Gives 16

 

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:

 

int d = c++ + c++ * 2;

which becomes:

int d = 5 + c++ * 2;

and c is incremented to 6 and we continue:

int d = 5 + 6 * 2;

and now we evaluate to get 17.

int d = c++ * 2 + c++;

which becomes

int d = 5 * 2 + c++;

and c is incremented to 6 and we continue:

int d = 5 * 2 + 6;

and now we evaluate to 16.

 

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;