Since we have now learnt what operations are, the next logical step is learn expressions. An expression is a statement that manipulates or modifies data using operations. So, we can say that it is a collection of operations.

Examples of Expressions

Here is an example of an expression -
a = b/10;
Here, we divide the value of b by 10 and assign the result to a. So, we manipulate variables using operations.
It is not necessary that an expression has to contain only one operation. It can have many in succession. Example -
 a = ++b + 5 + b;
There are a combination of operators in this case. There is an addition as well as an increment operator. It is important to note here that an expression is evaluated from left to right, i.e., the left most expression gets evaluated first followed by the one next to it and so on, until we reach the right most expression, where we stop.
So, let us find the value of this expression assuming that b = 1.
We begin from the left and end at the right.
  • ++b - Increment the value of b. So, is now 2.
  • + 5 - Add 5 to the value of b. Since b is now 2, it is 2+5 which is 7.
  • +b - Add the value of b to the above value we have obtained. Since, value of b is 2 and the above value is 7, so, 2+7 is 9.
Hence, the required value is 9.

Now, think. If the expression was evaluated from right to left what value do we obtain. Start with 5+b, which is 5+1=6. Now we do ++b=2 and add that value to 6. We get 8. Observe that this is not the same as the one we got when we evaluated left to right. If, you run this code, you will indeed find that the answer is 9, which confirms that the compiler evaluates form left to right.

Always remember that an expression is evaluated from left to right.

Let us look at an expression with two or more different operators. Example -

a = b+10/5
There is a + and a / operator, so, both of them are evaluated to give the value to a.
Well, actually, it's not that simple. We do not know how the computer will evaluate this. Does it divide first or does it add first.
Let us say that we gave b the value of 5 earlier. When we substitute 5, we get 5+10/5. If we add 5 and 10 we get 15 and if we divide 15 by 5 we get 3. So, the answer is 3, right?.
Not so fast. If we divide 10 by 5 we get 2 and if we add that to 5 we get 7.
So, we can get two different answers. The answer depends on whether we divide first or add first.
If you compile this with the haxe compiler, you will find out that the answer is 7. So, the compiler divides before adding. This is because, the division operator has a higher precedence than the addition operator.
We will see what this means now.

Operator Precedence

Let us take the example of this expression -
t = a * b % c && d - 4 / r + 2;
Looks complex !
So how does the compiler decide which operators' operation to do first. It does it using operator precedence.
Each operator has its own precedence or importance. When 2 or more operators are present in the same expression, then their precedence is compared. The one with greater precedence is evaluated first, the one with second greatest precedence is evaluated next and so on.

The precedence of the operators follows this order -


Thus, if a division and an addition are present together, then the division is evaluated first, followed by addition.

Exception : Right to Left Evaluation

Normally an expression is evaluated from left to right. But, if the expression contains =,<<,>> or ! then it is evaluated right to left. Let us take the example of equality -
a = 5*5;
If we neglect the equality then we evaluate the expression 5*5 from left to right. The value is 25. Now, when we consider the equality we say a = 25. So, a gets the value 25. Thus we are giving a value which is on the right side of the expression to the left side, i.e., we gave the value 25 ( present on the right of the equality ) to a ( present on the left side of the equality ). Thus when we included the equality, we evaluated right to left.
Thus, the part of the expression without the equality is evaluated left to right and the part of the expression with the equality is evaluated right to left.

Avoiding This Precedence Nonsense

If you are writing a long expression with a lot of operators, it is going to be a headache to see which operator has the most precedence. That's why you need to use brackets. Lets look at this example -
a = ( 2 * 5 ) / 3;
The compiler evaluates from the inner most bracket first. So, it first evaluates 2*5 which is present in the innermost bracket. This gives 10. Now, this number is divided with 3 to give 3.34.
Lets look at another more complex example -
a = ( ( ( 2 *5 ) / 2 ) + 8 ) -5
All you have to remember is to evaluate from the innermost bracket and then work your way outwards. So, the value of this expression would be 8.

The use of brackets can highly simplify a large expression. You should use always use them.