# Operator associativity

Operator associativity is used primarily in computer science , but also mathematics and logic :

1. in the narrower sense, the property of an operator that the order in which several occurrences of the operator are evaluated in an expression has no influence on the final result, i.e. the associative law applies to this operator ;${\ displaystyle *}$ ${\ displaystyle (a * b) * c = a * (b * c)}$
2. in a broader sense, the definition of how an operator that is not associative in the narrower sense should be evaluated.

For example, in mathematics, addition and normal multiplication in the narrower sense are associative operators, i.e. or , as well as matrix multiplication, and in logic, conjunction and disjunction . ${\ displaystyle (a + b) + c = a + (b + c)}$${\ displaystyle (a \ cdot b) \ cdot c = a \ cdot (b \ cdot c)}$ ${\ displaystyle (\ land)}$ ${\ displaystyle (\ lor)}$

In the case of operators that are not associative in the narrower sense , on the other hand, the result depends on the operator associativity defined by convention in the broader sense : In order to avoid that expressions with adjacent, equivalent operators without parentheses are ambiguous, it is determined whether the operators concerned are from left to are to be evaluated right or vice versa from right to left.

## Left associative operators

A left-associative operator is evaluated from left to right - a binary operator is therefore considered to be left-associative if the expressions ${\ displaystyle *}$

 ${\ displaystyle a * b * c}$ ${\ displaystyle: = (a * b) * c}$ ${\ displaystyle a * b * c * d}$ ${\ displaystyle: = {\ bigl (} (a * b) * c {\ bigr)} * d}$ Etc.

are to be evaluated as shown. Examples of left-associative operations are:

 Subtraction : ${\ displaystyle abc}$ ${\ displaystyle = (from) -c}$ Division : ${\ displaystyle a: b: c}$ ${\ displaystyle = (a: b): c}$ ${\ displaystyle a \ div b \ div c}$ ${\ displaystyle = (a \ div b) \ div c}$ ${\ displaystyle a / b / c}$ ${\ displaystyle = (a / b) / c}$ However: In the case of horizontal fraction lines, the shorter fraction line binds more strongly: ${\ displaystyle {\ frac {a} {\ frac {b} {c}}}}$ ${\ displaystyle = {\ frac {a} {{\ bigl (} {\ frac {b} {c}} {\ bigr)}}} = {\ frac {a \ cdot c} {b}}}$ ${\ displaystyle {\ frac {\ frac {a} {b}} {c}}}$ ${\ displaystyle = {\ frac {{\ bigl (} {\ frac {a} {b}} {\ bigr)}} {c}} = {\ frac {a} {b \ cdot c}}}$

## Legal associative operators

Conversely, a right-associative operator is evaluated from right to left, so that: ${\ displaystyle *}$

 ${\ displaystyle x * y * z}$ ${\ displaystyle: =}$ ${\ displaystyle x * (y * z)}$ ${\ displaystyle w * x * y * z}$ ${\ displaystyle: =}$ ${\ displaystyle w * (x * (y * z))}$ Etc.

Examples of right-associative operations are:

• The exponentiation : because it would be easy . Attention: calculator cost inputs the shape however usually left-associative, so look as if they in the form were entered - in terms of this form therefore are right-potentiating must always through its own Parentheses are enforced: .${\ displaystyle x ^ {y ^ {z}}: = x ^ {(y ^ {z})}}$${\ displaystyle (x ^ {y}) ^ {z}}$${\ displaystyle x ^ {yz}}$
x ^ y ^ z(x ^ y) ^ zx ^ (y ^ z)
• The subjunction in logic is used by most authors rechtssassoziativ, which means that as we read.${\ displaystyle P \ rightarrow Q \ rightarrow R}$${\ displaystyle P \ rightarrow (Q \ rightarrow R)}$
• The assignment operator of some programming languages , such as C :, x = y = zis synonymous with x = (y = z), that is, the value of yis first zassigned to the variable and only then is the result of this assignment (i.e. the assigned value z) xassigned to the variable .

## Side effects in programming languages

In programming languages ​​that allow side effects in expressions, the order in which these side effects are performed / effective is important. Some programming languages ​​strictly define this evaluation sequence, others (such as C or C ++ ) leave the evaluation sequence undefined for most of the Infix operands. Example:

 int f1(void); int f2(void); int f3(void); int g(int); int h(void) { return g( f1() - f2() - f3() ); }  It is not defined whether to run first f1, f2or f3(and for its side effects to take effect).