Tutorial#12 - Order of Precedence

Order of Precedence

Order of Precedence:

When more than one arithmetic operator is used in an expression, C++ uses the operator precedence rules to evaluate the expression. According to the order of precedence rules for arithmetic operators, *, /, and % are at a higher level of precedence than + and -.
Note that the operators *, /, and % have the same level of precedence. Similarly, the operators + and - have the same level of precedence.
When operators have the same level of precedence, the operations are performed from left to right. To avoid confusion, you can use parentheses to group arithmetic expressions. For example, using the order of precedence rules:
3 * 7 - 6 + 2 * 5 / 4 + 6
means the following:
(((3 * 7) - 6) +((2 * 5) / 4)) + 6
= ((21 - 6) + (10 / 4)) + 6     (Evaluate *)
= ((21 - 6) + 2) + 6             ( Evaluate /. Note that this is an integer division.)
= (15 + 2) + 6             (Evaluate -)
= 17 + 6                     (Evaluate first +)
= 23             (Evaluate +)

Comments