Information Technology Reference
In-Depth Information
Category
Operators
?:
Conditional
= *= /= %= += -= <<= >>= &= ^= |=
Assignment
Associativity
If all the operators in an expression have different levels of precedence, then evaluate each
sub-expression, starting at the one with the highest level, and work down the precedence scale.
But what if two sequential operators have the same level of precedence? For example,
given the expression 2 / 6 * 4, there are two possible evaluation sequences:
(2 / 6) * 4 = 4/3
or
2 / (6 * 4) = 1/12
When sequential operators have the same level of precedence, the order of evaluation is deter-
mined by operator associativity . That is, given two operators of the same level of precedence, one or
the other will have precedence, depending on the operators' associativity. Some important charac-
teristics of operator associativity are the following, and are summarized in Table 8-5:
￿
Left-associative operators are evaluated from left to right.
￿
Right-associative operators are evaluated from right to left.
￿
Binary operators, except the assignment operators, are left-associative.
￿
The assignment operators and the conditional operator are right-associative.
Therefore, given these rules, the preceding example expression should be grouped left to
right, giving
(2 / 6 ) * 4, which yields 4/3.
Table 8-5. Summary of Operator Associativity
Type of Operator
Associativity
Assignment operators
Right-associative
Other binary operators
Left-associative
The conditional operator
Right-associative
Parenthesized Expressions
You can explicitly set the order of evaluation of the sub-expressions of an expression by using
parentheses. Parenthesized sub-expressions
￿
Override the precedence and associativity rules
￿
Are evaluated in order from the innermost nested set to the outermost
Search WWH ::




Custom Search