Operator associativity

from Wikipedia, the free encyclopedia

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 ;
  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 .

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


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

      However: In the case of horizontal fraction lines, the shorter fraction line binds more strongly:

Legal associative operators

Conversely, a right-associative operator is evaluated from right to left, so that:


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: .
    x ^ y ^ z(x ^ y) ^ zx ^ (y ^ z)
  • The subjunction in logic is used by most authors rechtssassoziativ, which means that as we read.
  • 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).

See also

Individual evidence

  1. ^ Order of operations . (PDF) Rochester Institute of Technology
  2. ^ The Order of Operations. Education Place
  3. ^ The Order of Operations . Khan Academy ( video, from 05:40 am )
  4. ^ Using Order of Operations and Exploring Properties . (PDF) Section 9 Virginia Department of Education
  5. Precedence rules and associativity . Chemnitz University of Technology
  6. ^ Rules for Exponents and the Reasons for Them . (PDF) Western Michigan University