Hierarchy of Operations
When their are more than one operators are there in one statement then in which order the operations will be performed is decided by the hierarchy of the operations.
The priority or precedence in which the operations in an arithmetic statement are performed is called the hierarchy of operations.
Priority | Operators | Description |
---|---|---|
1st | * / % | multiplicatio, division, modular division |
2nd | + - | addition, subtraction |
3rd | = | assignment |
Rule -1 Within parenthesis ( ) the same hierarchy is used as shown in the above figure.
Rule - 2 If there are more than one set of parentheses, the operations within innermost parentheses would be performed first, followed by the operations within the second innermost pair and so on.
Rule - 3 We must always remember to use pair of parentheses. ( opening and closing parenthesis )
Example - 1 Evaluation of expression in C (assume i to be an integer)
i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8
i = 6 / 4 + 4 / 4 + 8 -2 + 5 / 8 ( * )
i = 1 + 4 / 4 + 8 -2 + 5 / 8 ( / )
i = 1 + 1 + 8 - 2 + 5 / 8 ( / )
i = 1 + 1 + 8 -2 + 0 ( / )
i = 2 + 8 -2 + 0 ( + )
i = 10 - 2 + 0 ( + )
i = 8 + 0 ( - )
i = 8 ( + )
Example -2 Evaluation of the following expression in C
k = 3 / 2 * 4 + 3 / 8 + 3
k = 1 * 4 + 3 / 8 + 3 ( / )
k = 4 + 3 / 8 + 3 ( / )
k = 4 + 0 + 3 ( / )
k = 4 + 3 ( + )
k = 7 ( + )
Last modified: Wednesday, 17 November 2021, 3:49 PM