The following table explains the precedence of the arithmetic operators:       

Operators

Operations

Precedence (Order of evaluation)

( )

Parentheses

Evaluated first

*, /, or %

Multiplication, Division, Modulus

Evaluated second.  If there are several, they are evaluated from left to right

+ or -

Addition, Subtraction

Evaluated last. If there are several, they are evaluated from left to right

Lets look some examples.

What is the result of   10 + 10 * 5 ?

The answer is 60 not 100. As * has higher precedence than + so 10 * 5 is evaluated first and then the answer 50 is added to 10 and we get the result 60. The answer will be 100 if we force the addition operation to be done first by putting 10 + 10 in parentheses. Thus the same expression rewritten as (10 + 10) * 5 will give the result 100. Note that how the parentheses affect the evaluation of an expression.

Similarly the expression 5 * 3 + 6 / 3 gives the answer 17, and not 7. The evaluation of this expression can be clarified by writing it with the use of parentheses as (5 * 3) + (6 / 3) which gives 15 + 2 = 17. Thus you should be careful while writing arithmetic expressions.

 

 

 

Previous

 

 

TOC

 

 

Next