precedence

The awk programming language uses rules of *precedence* to determine the order in which operators within an [[expression?]] are used during evaluation. The order that operators are evaluated within an [[expression?]] are predetermined by the intepreter and the operators with the highest priority are evaluated first. An [[neophite?|inexperienced programmer]] may expect the following programming line to produce a result of 35. However, this actually produces a value of 23, because multiplication has a higher precedence than [[addition?]], so it is performed first.

print 3 + 4 * 5    # This produces 23 because multiplication is done before addition

_Using parentheses to control the order of evaluation_

It is possible to change the order in which operators within an [[expression?]] are evaluated by using parentheses enclosures. In the following example, the [[expression?]] produces a value of 35, because the parentheses have a higher precedence than multiplication, so their contents are evaluated first:

print (3 + 4) * 5    # The parentheses cause the addition to be evaluated before the multiplication

_Groups of operators or functions within parentheses have the same precedence as normal_

Groups of operators or [[function?]]s within parentheses have the same precedence as they normal would. In the following example, the multiplication within the parentheses occurs before the [[addition?]]:

print 3 * (4 + 2 * 3)   # Within the parentheses 2 * 3 is evaluated before 4 is added

_Operators of equal precedence are evaluated from left to right_

Operators of equal precedence are evaluated from left to right. In the following example, the [[addition?]] and [[subtraction?]] operators have equal precedence, so the operators are evaluated from left to right. This means that the [[subtraction?]] takes place before the [[addition?]], because the [[subtraction?]] operator occurs on the left side of the [[expression?]] before the [[addition?]]:

print 5 - 2 + 1

_Side Effects_

An expression that changes the value of one of its components during evaluation, may produce [[side_effects?]] and the results that it produces may vary depending on which version of awk is being used. In the following example code, the awk interpreter may produce a value of 8 or 9, depending on whether or not the [[assignment?]] in parentheses affects the value outside.

BEGIN {
  # Changing the number variable in the middle of the expression may introduce side effects
  result = (number = 4) + number    # This expression may produce a value of 8 or 9 depending on the awk version
  print "The result is " result 
}

Within an expression, each operator has a predetermined precedence

Within an [[expression?]], each operator is has a predetermined position, precedence, and [[associativity?]].

Operator Position

The position of the operator is the position of operator as a component within the [[expression?]], and relates to other components within the [[expression?]]:

12 + (2 x -4) + 5^2

The above [[expression?]] contains the following [[component?]]s:

Position Component Notes
112The first component is the number 12. This is also the first element
2+The second component is the [[plus?]] operator. This is the first operator
3(The third component is the opening bracket. The expression (2 x !-4) is the second element
42The fourth component is the number 2. This is the first element of the second element
5x The fifth component is the multiplication operator. This is the first operator in the second element
6-The sixth component is the [[minus?]] operator. This is the second operator in the second element
74The seventh component is the number 4. This is the second element of the second element
8)The eighth component is the closing bracket.
9+The ninth component is the [[plus?]] operator. This is the sixth operator.
105The tenth component is the number 5. The expression 5^2 is the third element
11^The eleventh component is the [[caret?]] exponent operator. This is the seventh operator
122The twelfth component is the number 2. This is the third component of the third expression

Prefix, Postfix, Infix

An operator is either a [[prefix?]] operator, a [[postfix?]] operator or an [[infix?]] operator depending upon is positioning within the [[expression?]].

Prefix Operators

A [[prefix?]] operator immediately precedes its [[operand?]]. The [[unary?]] operators used to indicate whether a number is [[positive?]] or [[negative?]] is a [[prefix?]] operator:

Postfix Operators

A [[postfix?]] operator immediately follows its [[operand?]] within an expression.

Infix Operators

An [[infix?]] operator is positioned between its left and right [[operand?]]s:

Precedence

The precedence of an operator is determined by a numerical [[value?]] representing the priority of the operator whilst determining the order in which the operators will be evaluated. Note that operators to be evaluated first have a higher value.

Competing for Operands

Operands go to the operator with the highest level of precedence

In cases where two operators of different precedences compete for the same [[operand?]], the [[operand?]] belongs to the operator with the highest precedence:

3 + 4 * 5

In the above expression, both the [[plus?]] operator and the multiplication operator are competing for the second element (numeral 4) as an [[operand?]]. Because the multiplication operator has the higher precedence, it owns the [[operand?]], so the number 4 is associated with the multiplication operator, rather than the [[addition?]] operator.

Operators of equal precedence are evaluated from left to right

In cases where two operators of the same precedence compete for [[operand?]]s, the [[operand?]] belongs to the operator on the left:

6 + 5 - 3

In the above expression, because both the [[addition?]] and [[subtraction?]] operator have equal precedence, the second element (numeral 5) belongs to the addition operator, so the [[expression?]] is evaluated mathematically as:

(6 + 5) - 3