# Operator Precedence and Associativity in C++ and PHP

## Intro

Learning C++ so far has been an interesting journey for me. I am discovering how data is being manipulated and what really happens.

For today’s post, I would talking about **Operator Precedence and Associativity**.

### What is Operator Precedence and Associativity?

Operator Precedence and Associativity are set of rules that tells us how different operators (eg + / * - %) are evaluated in an expression.

I wouldn’t say, I fully understand it yet, but from my little knowledge so far, I think I have the hang of it

## Examples

Here are some examples I did using C++ and PHP

### C++

```
#include <iostream>
int main(){
int a {6};
int b {3};
int c {8};
int d {9};
int e {3};
int f {2};
int g {5};
// Example 1
int result = a + b * c -d/e -f + g;
// 6 + (3 * 8) - (9 / 3) - 2 + 5
// 30 - 3 + 3
// 30
std::cout << "result : " << result << std::endl;
// Example 2
result = a / b * c + d - e + f;
// 2 * 8 + 9 - 3 + 2
// 16 + 9 - 1
// 24
std::cout << "result : " << result << std::endl;
// Example 3
result = a + (b * c) -(d/e) -f + g;
// 6 + 24 - 3 - 2 + 5
// 30 - 5 + 5
// 30
std::cout << "result () : " << result << std::endl;
// Example 4
result = (a + b) * c -d/e -f + g;
// 9 * 8 -3 -2 + 5
// 72 - 5 + 5
// 72
std::cout << "result () : " << result << std::endl;
return 0;
}
```

```
<!-- Output -->
Example 1 : 30
Example 2 : 24
Example 3 : 30
Example 4 : 72
```

### PHP

```
<?php
$a = 6;
$b = 3;
$c = 8;
$d = 9;
$e = 3;
$f = 2;
$g = 5;
// Example 1
$result = $a + $b * $c -$d/$e -$f + $g;
// 6 + (3 * 8) - (9 / 3) - 2 + 5
// 30 - 3 + 3
// 30
echo $result;
// Example 2
$result = $a / $b * $c + $d - $e + $f;
// 2 * 8 + 9 - 3 + 2
// 16 + 9 - 1
// 24
echo $result;
// Example 3
$result = $a + ($b * $c) -($d/$e) -$f + $g;
// 6 + 24 - 3 - 2 + 5
// 30 - 5 + 5
// 30
echo $result;
// Example 4
$result = ($a + $b) * $c -$d/$e -$f + $g;
// 9 * 8 -3 -2 + 5
// 72 - 5 + 5
// 72
echo $result;
?>
```

```
<!-- Output -->
Example 1 : 30
Example 2 : 24
Example 3 : 30
Example 4 : 72
```

## Analysis

The **BODMAS RULE (Bracket Order Division Multiplication Addition Subtraction)**
has been the mathematical rule for a very long while.
In the example 3 and 4 followed the BODMAS rule, and the brackets was calculated first
before joining them with the order operators in the expression.
I have been following this order for a very long time while coding. I just add paranthesis **()**
and it makes it readable for me without understanding what actually happens.

### Left Associativity Rule

In examples 1 and 2, we follow the rule of operator precedence. In the operator precedence order,
**Multiplication, Division (*, /)** is higher than **Addition and Subtraction (+, -)**.
What that means is that Multiplication and Division would be done first before Addition and Subtraction.
But there are some situations where you would want to enforce a particular operator to be evaluated first,
and how to enforce that is by using **paranthesis ()**.

Adding Paranthesis, makes the values to be calculated first in its little scope, before joining the parent scope. Examples 3 and 4 are examples of such occurences. In this situation, we would say, it almost looks like the BODMAS rule, I’ve been following. Yes, it does.

In Operator Precedence and Associativity, there are also some certain rules to bare in mind.
I have currently only used **Left Associativity**

### Meaning of Left Associativity?

Left Associativity is simply calculating from Left - to - right. All the examples are following this rules. Let me illustrate it a little bit.

```
<!-- Example 1 -->
result = -2 + 4 - 5
<!-- Example 2 -->
result2 = 30 / (4 -5) * 6
```

In example 1, we would start from the left side i.e -2 + 4 = 2, 2 - 5 = -3.

So the output for example 1 would be -3

In example 2, we would also start from the left, but now we would remember the paranthesis.
Priority has been assigned to the paranthesis, in as much as Multiplication and Division is higher
than Subtraction in the Preference Order.
So what we would have is: 4 - 5 = -1, 30 / -1 = -30, -30 * 6 = -180

So the output for example 2 would be -180

```
<!-- Output -->
echo result = -3
echo result2 = -180
```

## Summary

Operator Precedence and Associativity is good, especially the ordering. But for the readability of code, it is advisable to use paranthesis. Whatever approach is still good.

I keep on learning and improving, I think ^-^

## Leave a comment