2010-03-30 Jb Evain <jbevain@novell.com>
[mcs.git] / docs / ecma334 / 14.2.1.xml
blob99c481815dde66f229fefc73017ff521f354880e
1 <?xml version="1.0"?>
2 <clause number="14.2.1" title="Operator precedence and associativity">
3   <paragraph>When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated. <note>[Note: For example, the expression x + y * z is evaluated as x + (y * z) because the * operator has higher precedence than the binary + operator. end note]</note> The precedence of an operator is established by the definition of its associated grammar production. <note>[Note: For example, an <non_terminal where="14.7">additive-expression</non_terminal> consists of a sequence of <non_terminal where="14.7">multiplicative-expression</non_terminal>s separated by + or  -operators, thus giving the + and  -operators lower precedence than the *, /, and % operators. end note]</note> </paragraph>
4   <paragraph>The following table summarizes all operators in order of precedence from highest to lowest: <table_line>Section Category Operators </table_line>
5 <table_line>14.5 Primary x.y f(x) a[x] x++  x--new </table_line>
6 <table_line>typeof checked unchecked </table_line>
7 <table_line>14.6 Unary +  -! ~ ++x --x (T)x </table_line>
8 <table_line>14.7 Multiplicative * / % </table_line>
9 <table_line>14.7 Additive +  </table_line>
10 <table_line>-14.8 Shift &lt;&lt; &gt;&gt; </table_line>
11 <table_line>14.9 Relational and </table_line>
12 <table_line>type-testing </table_line>
13 <table_line>&lt; &gt; &lt;= &gt;= is as </table_line>
14 <table_line>14.9 Equality == != </table_line>
15 <table_line>14.10 Logical AND &amp; </table_line>
16 <table_line>14.10 Logical XOR ^ </table_line>
17 <table_line>14.10 Logical OR | </table_line>
18 <table_line>14.11 Conditional AND &amp;&amp; </table_line>
19 <table_line>14.11 Conditional OR || </table_line>
20 <table_line>14.12 Conditional ?: </table_line>
21 <table_line>14.13 Assignment = *= /= %= += -= &lt;&lt;= &gt;&gt;= &amp;= ^= |= </table_line>
22 When an operand occurs between two operators with the same precedence, the associativity of the operators controls the order in which the operations are performed: <list><list_item> Except for the assignment operators, all binary operators are left-associative, meaning that operations are performed from left to right. <example>[Example: For example, x + y + z is evaluated as (x + y) + z. end example]</example> </list_item><list_item> The assignment operators and the conditional operator (?:) are right-associative, meaning that operations are performed from right to left. <example>[Example: For example, x = y = z is evaluated as x = (y = z). end example]</example> </list_item></list></paragraph>
23   <paragraph>Precedence and associativity can be controlled using parentheses. <example>[Example: For example, x + y * z first multiplies y by z and then adds the result to x, but (x + y) * z first adds x and y and then multiplies the result by z. end example]</example> </paragraph>
24 </clause>