An expression is evaluated based on the operator precedence and associativity. When there are multiple operators in an expression, they are evaluated according to their precedence and associativity. The operator with higher precedence is evaluated first and the operator with the least precedence is evaluated last.
Let us consider the following simple example expression...
18 + 10 * 5 / 2
In the above expression, there are three operators +, * and /.
According to the operator precedence both multiplication and division have the same higher precedence and addition has lower precedence. Multiplication and division have the same precedence they are evaluated based on the associativity. The associativity of multiplication and division is left to right. So, multiplication is performed first, then division and finally addition.
10 * 5 ====>50
50 / 2 ====>25
18 + 25 ===>43
The expression is evaluated to 43.
No comments:
Post a Comment