Examples on Arithmetic Operators :
a = 5 + 6; ====> Answer is 11 and 11 is assigned to a (a is Integer Variable Here).
a = 10 – 5; ====> Result is 5 and 5 is assigned to a.
a = 5 * 10; ====> Result is 50 and 50 stored in a now.
But if we contains more than one operator in an Expression like below example.
a = 5 – 3 + 2 ; ????
How to calculate this Expression and What is the value of a (i.e Result) ???? if you have this doubt you are in correct place.
How to Evaluate an Expression which have Morethan One Operator :
- Whenever an Expression Contains more than one Operator, how to evaluate that Expression ?. Solution is Precedence of Operators (priority of operators).
- C operators have priority that means if we have morethan one Operator in Expression priority of operator will decides which operator evaluated first and which one is last.
- According to Priority which operator have highest priority that one should be Evaluated First similarly the Operator which have lowest priority will be Evaluated Last.
- We are here discussing about Arithmetic operators so here is the priorities of Arithmetic Operators You can see full Priority table Here.
- From above table we can see that Arithmetic operators have two priority levels.
- Multiplication( * ) , Division( / ) and Modulo Division( % ) have same priority.
- Subtraction( – ) and Addition( + ) Have same Priority.
- Whenever we have morethan one operator and all operators have same priority then Associativity of operator will decides which one to evaluate First.
- All Arithmetic Operators have Associativity from Left to Right.
Quick Example on Associativity :
If we have a Expression that contains two operators and those are multiplication and Division. First of all we need check the priority Rank of the operators. here two operators have same priority,.. Now Operators Associativity comes into picture. As we know all arithmetic operators have left to Right Priority this Expression can be Evaluated from Left to Right.
a = 5 * 10 / 5 ;
step 1 : Checking priorities of Operators , Two are have same priority.
step 2 : Associativity of Operators, Here Arithmetic Operators so Left to Right.
so first 5*10 will calculated result is 50.next 50/5 will calculated the Result is 10.
Like this way we can calculate any kind of Expression in Simple way by following priority and Associativity of Operators.
Few More Examples :
a = 8 + 2 – 5 ; ====> Result a = 5;
a = 100 * 200 / 100 * 100 + 100; ====> Result is a = 20100;
a = 20+30*10-50/10%2; ======> Result is a = 319;
We have all operators so fist high priority operators are evaluated those are * , / and %. But we have three same priority operators so according to Associativity left to Right will be Calculated. then + and – will be Evaluated. here is the step by step Explanation of above expression’s Evaluations.
Step 1 : 30 * 10 calculated result is 300. after first calculation Expression will remains as below
a = 20 + 300 – 50 / 10 % 2 ;
Step 2 : 50 / 10 = 5; after step 2 expression is like this
a = 20 + 300 – 5 % 2;
Step 3 : 5 % 2 = 1. because modulo division gives Remainder so after this step expression will be
a = 20 + 300 – 1 ;
step 4 : Now we have two low priority operators and these associativity also Left to Right so first + is calculated then subtraction is performed.
20 + 300 = 320;
a = 320 -1 ;
step 5 : 320 – 1 =319 ;
a = 319;
That’s it we got our Answer… By following this simple priority and Associativity we can Calculate Very Complex Expressions..
Also Read :