 # Expression Evaluation

Java expressions and Arithmetic expressions evaluate in the same way.

So the rules of arithmetic apply the same when used in Java. The only difference is the format. For example the arithmetic expression: translates to the Java expression `(2 + 3x)/4 - 5(y-6)(a + b + c)/x + 7(8+x)/y` .

As I said before, the same arithmetic rules apply for Java expressions. So here are the rule that you need to know:

Order of evaluations:

1. What’s inside the parenthesis must be evaluated first.
2. Parenthesis can be nested so that the inner parenthesis are evaluated first.
3. Multiplication, Division and Remainder operators are evaluated after
4. Lastly, addition and subtraction operators are evaluated last.

# Assignment Operator Augmentation

Combine +, -, *, /, and % Operators with the Assignment operator to make an augmented operator.

It is a common occurrence in many Java programs that the value of a variable is used, changed, but then reassigned back to the same variable. For example, here’s a statement that increases a variables `count` by `1`:

`count = count + 1;`

In Java you can combine the addition operator and the assignment operator into an augmented operator to do and equal the same as the statement above. Continuing the example:

`count += 1;`

`+=` is called an addition assignment operator. Down below I show the other augmented assignment operators:

```Operator    Examples    Equivalent
+=          x += 7      x = x + 7
-=          x -= 7      x = x - 7
*=          x *= 7      x = x * 7
/=          x /= 7      x = x / 7
%=          x %= 7      x = x % 7```

Notice that augmented operators is done after all other operations are evaluated in an expression. For example, this:

`x *= 3 + 4.5 / 2.5;`

means this:

`x = x * (3 + 4.5 / 2.5);`

Also note that augmented assignment operators don’t have spaces in between them. So `+ =` is wrong, it should be `+=`.

# Increment & Decrement Operators

You can increase a variable by 1 with the increment operator `++`, or decrease by 1 with the decrement operator `--`. These operators are extremely useful because in many programming tasks, you only need to change a variables value by 1, like with Loops. Here are example of incrementation and decrementation operations:

```int x = 4, y = 4;
x++; // x plus plus causes x to becomes 5
y--; // y minus minus causes y becomes 3```

When the increment and decrement operators are placed after a variable, they are called posfix increments/decrements; like in the above example. When increment and decrement operators are placed before a variable, they are called prefix increments/decrements, as I show in the below example:

```int x = 4, y = 4;
++x; // plus plus x causes x to becomes 5
--y; // minus minus y causes y becomes 3```

Note that you see the same effect regardless of the variables and operators order in these examples. However the order of variable and operator do have different effects in other situations:

```Operator    Name            Description                                    Examples, assuming x = 1
++Var       preincrement    Increment by 1, new var value is used          int y = ++x; // y is 2, x is 2
var++       postincrement   Increment by 1, original var value is used     int y = x++; // y is 1, x is 2
--var       predecrement    Decrement by 1, new var value is used          int y = --x; // y is 0, x is 0
var--       postdecrement   Decrement by 1, original var value is used     int y = x--; //y is 1, s is 0```

## Example 1

Here’s an example showing how prefixing the operator is significantly different from postfixing the operator:

```int x = 5;
int newNum = 5 * x++;
System.out.print("x is " + x + ", new number is " +newNum);```

Which is the same as:

```int x = 5;
int newNum = 5 * x;
x = x + 1;
System.out.print("x is " + x + ", new number is " +newNum);```

Output: Notice that switching the order of the increment operator to prefix changes the evaluation:

```int x = 5;
int newNum = 5 * ++x;
System.out.print("x is " + x + ", new number is " +newNum);```

Which is the same as:

```int x = 5;
x = x + 1;
int newNum = 5 * x;
System.out.print("x is " + x + ", new number is " +newNum);```

Notice that the incrementation occurred before the evaluation, meaning that the output is affected:

```double a = 1.0;
After all 3 lines are executed, `a = 0.0`, `b = 6.0`, and `c = 7.0`.