Operator precedence

from Wikipedia, the free encyclopedia

As operator precedence , -wertigkeit , -Priority or -präzedenz is called in mathematics , logic and computer science , a defined partial order in which the operators of an in infix notation this expression to be evaluated.

It is not a total order, but a partial order, because there is no strict order between all operators. There can also be several of the same rank. For example, the rank of multiplication and division is the same, but higher than the rank of addition and subtraction (" point calculation before line calculation "). You can save explicit brackets through the ranking. So is synonymous with because the multiplication operator has a higher rank. However, this is always a matter of definition.

A bracketing offers the possibility of override of a section of a chain of listed operations: The bracketed, ie a set of parentheses "(...)" enclosed area is computationally first run and replace it with the corresponding partial result, making the brackets obsolete, as it no longer contains any operators. The brackets must contain the operators and their necessary operands.

If it is noted, the expression in brackets must first be calculated, i.e. the sum must be formed before multiplying with this sum.

Strictly speaking, the additional convention is required that expressions must be evaluated from left to right in order to determine the exact calculation sequence.

Priority of different operators

For the standard arithmetic operations in mathematics, the following order of precedence is usual (in descending order of priority):

  1. Exponentiation
  2. Multiplication and division ("point calculation")
  3. Addition and subtraction ("line calculation")

There are additional categories in programming languages ​​and computer programs for evaluating formulas (e.g. the Unix utility program bc ). One of them is the sign , which usually has an even higher priority than exponentiation. In mathematical formulas the expression is read as , but in the expressions of such evaluation programs it is often read as .

It is not always common in logic to define an operator precedence. Where this happens, the following is usually chosen (in descending priority):

  1. negation
  2. conjunction
  3. Disjunction
  4. Conditional
  5. Biconditional

After applying the mentioned operator precedence z. B. the arithmetic expression evaluated as , the logical expression as .

Order of equivalent operators

In addition, an associativity can be specified for operations , which determines the order in which adjacent, equivalent operators are to be evaluated. An operator is called left-associative if A op B op C op D is evaluated as ((A op B) op C) op D; an operator is called right-associative if A op B op C op D is evaluated as A op (B op (C op D)). Of the arithmetic operators mentioned, the exponentiation is defined as right-associative, i.e. H.:


Likewise the arrow operator :

The remaining two-digit operators are defined as left-associative, i.e. H. for example .

In the logic connectives usually defined left-associative, but there are certainly also authors who use at least the conditional right-associative.

There are also programming languages ​​such as B. Occam , which put all operators on the same rank and evaluate from left to right.

Outline signs

In order to change the defined operator precedence within an expression and to make expressions unique if a precedence is not defined, different forms of outline characters are used. In mathematics and also in modern logic, these are brackets.

In the history of logic, punctuation was often used, especially at the beginning, in order to prevent the ambiguity of expressions or to change a possibly defined operator precedence. A puncturing system, which gained a certain distribution through the basic work Principia Mathematica , was to be found more frequently in introductory logic books in variously modified forms until the 1970s. In this system, instead of the imaginary closing bracket before a juncture or instead of the imaginary opening bracket after a junctor, a period is placed. For example would be held

the following expression is written:

If several imaginary brackets meet, two or more periods are set instead of a single point, so that instead of, for example

the following expression would be written:

Another system weights the junctures occurring in an expression with points. The more points there are above an operator, the weaker its bond strength, that is, the lower its priority. According to this system, for example, a conjunction with two points would bind weaker than a conjunction with a point, which in turn would be weaker than a conjunction without points.


In programming languages , a weighting and associativity are usually specified for all operators so that the structure of all expressions is unique even if no parentheses are used. The evaluation of an expression without side effects therefore always gives the same value.

Operator precedence is usually only important for expressions in infix notation . Spellings like the Polish notation or the reverse Polish notation were developed so that the expressions are unambiguous without precedence and without brackets. Even with the first predicate logic notation, the conceptual notation and with graphical notations such as the existential graph , the reading of an expression is clearly defined and does not require any structural symbols or conventions.

In programming languages ​​that allow side effects in expressions, the order in which they are carried out or take effect 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(int);

int g(void) { return f3( f1() * f2() ); }

It is not defined whether f1 or f2 is executed first (and thus "side effects" occur). In contrast, it is stipulated that f3 is only called after f1 and f2 (with their "side effects") have ended.


Individual evidence

  1. Jürgen Weiß: Taschenbuch der Mathematik ( Verlag Harri Deutsch and BG Teubner Verlagsgesellschaft ), ISBN 3-87144-492-8 , Volume 1, pp. 115-120, Chapter
  2. ^ George Mark Bergman: Order of arithmetic operations
  3. ^ Education Place: The Order of Operations