# Precedence and Associativity.ppt by ert554898

VIEWS: 8 PAGES: 11

• pg 1
```									            Precedence and Associativity

• The sequence in which different operators in an
expression are executed is determined by the
precedence of the operators.
• Operators with a higher precedence are executed
before operators with a lower precedence.
• In the following table, operators are in descending
order of precedence, so those with the highest
precedence are at the top.
• Operators within the same group in the table are of
equal precedence.

Precedence and            tMyn                          1
Associativity
• The sequence of execution of operators with the
same precedence in an expression is determined
from their associativity.
• The associativity of an operator determines how it
groups with its operands in an expression.
• Operators may be left-associative, right-
associative or non-associative.

Precedence and            tMyn                         2
Associativity
• All unary operators and all assignment operators are
right associative.
• Almost all other operators are left associative.
• With the throw operator (exception throwing) the
associativity is not available.

Precedence and            tMyn                           3
Associativity
• Associativity is only needed when the operators in an
expression have the same precedence. Usually +
and - have the same precedence. Consider the
expression 7-4+2. The result could be either (7-4)+2
= 5 or 7-(4+2) = 1. The former result corresponds to
the case when + and - are left-associative. The latter
result corresponds to the case when + and - are right-
associative. Operators with the same precedence
always have the same associativity. The operators +,
-, * and / are left-associative.

Precedence and            tMyn                         4
Associativity
• A left-associative operator can be said to associate
"to the left", and similarly a right-associative operator
can be said to associate "to the right". To understand
the intuition behind these names, consider again the
expression 7-4+2. If + and - are left-associative then
the middle operand (4) belongs to the operator on its
left (hence the name "to the left"). If + and - are right-
associative then the middle operand belongs to the
operator on its right (hence the name "to the right").

Precedence and              tMyn                               5
Associativity
• A left-associative operator may also be said to have
"left to right" associativity, and a right-associative
operator may also be said to have "right to left"
associativity. This is somewhat counter-intuitive
considering the above paragraph. To understand the
intuition behind these names consider the expression
1+2+3+4+5. If + is left-associative, the addition
operations are carried out left to right, i.e. the result is
(((1+2)+3)+4)+5. If + is right-associative, the addition
operations are carried out right to left, i.e. the result is
1+(2+(3+(4+5))).

Precedence and               tMyn                            6
Associativity
Operator           Name or Meaning        Associativity
::                 Scope resolution       Left to right
.                  Direct member          Left to right
selection
->                 Indirect member        Left to right
selection
[]                 Subscript              Left to right
()                 Function call          Left to right
++                 Postfix increment      Left to right
--                 Postfix decrement      Left to right
static_cast        Explicit static cast   Left to right
dynamic_cast       Dynamic cast           Left to right
const_cast         Cast away const        Left to right
reinterpret_cast   Unchecked cast         Left to right
typeid             Type identification    Left to right

Precedence and               tMyn                          7
Associativity
Operator        Name or Meaning             Associativity
+               Unary plus                  Right to left
-               Unary minus                 Right to left
++              Prefix increment            Right to left
--              Prefix decrement            Right to left
!               Logical negation - not      Right to left
~               Bitwise complement          Right to left
*               Dereference                 Right to left
(type)          Explicit cast (old style)   Right to left
sizeof          Size of object or type      Right to left
new             Allocate memory             Right to left
delete          Deallocate memory           Right to left

Precedence and            tMyn                               8
Associativity
Operator        Name or Meaning               Associativity

.*              Direct pointer-to- member     Left to right
selection

->*             Indirect pointer-to- member   Left to right
selection

*               Multiply                      Left to right

/               Divide                        Left to right

%               Modulus                       Left to right

+               Binary addition               Left to right

-               Binary subtraction            Left to right

<<              Shift left                    Left to right

>>              Shift right                   Left to right

Precedence and                        tMyn                     9
Associativity
Operator        Name or Meaning         Associativity

<               Less than               Left to right

<=              Less than or equal to   Left to right

>               Greater than            Left to right

>=              Greater than or equal   Left to right
to

==              Equal to                Left to right

!=              Not equal to            Left to right

&               Bitwise AND             Left to right

^               Bitwise exclusive OR    Left to right

|               Bitwise OR              Left to right

Precedence and              tMyn                         10
Associativity
Operator            Name or Meaning        Associativity

&&                  Logical AND            Left to right

||                  Logical OR             Left to right

?:                  Conditional operator   Right to left

=                   Assignment             Right to left

*= /= %= += -= &=   Compound assignment    Right to left
^= |= <<= >>=

Throw               Throw exception        Left to right

,                   Comma                  Left to right

Precedence and               tMyn                           11
Associativity

```
To top