Sunday, January 8, 2012

Operator Precedence

There is one ''gotcha'' to the use of the built-in operators: When multiple operators are combined in a single
expression, the order of expression evaluation is determined by a predefined precedence level for each
operator. For example, the result of 5+2*10 is always 25 and never 70 because the multiplication operator
has a higher precedence level than that of addition; as a result, 2 is always multiplied by 10 before the
addition of 5.
We can override the built-in precedence level by placing parentheses around the operators we wish to be
evaluated first. (5+2)*10 , for example, evaluates to 70.
For the operators I've introduced, the precedence order is listed next. An operator has a higher precedence
than an operator under it. Operators on the same line have equal precedence. In these cases, the order of
evaluation is left to right.


Running Codes
logical NOT
arithmetic (*, /, %)
arithmetic (+, -)
relational (<, >, <=, >=)
relational (==, !=)
logical AND
logical OR
assignment


No comments:

Post a Comment