Lua Programming/How to Lua/precedence

The lua programming language uses rules of precedence to determine the order in which operators within an expression are evaluated. For example, look at the following programming line:

print 3 + 4 * 5

An inexperienced programmer may expect a result of 35 to be obtained from the expression. However, this actually produces a value of 23, because multiplication has a higher precedence than addition, so it is performed first.

Within an expression has a predetermined precedenceEdit

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

Operator PositionEdit

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 components:

| Position | Component | Notes | 1 | 12 | The 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 | 4 | 2 | The fourth component is the number 2. This is the first element of the second element | 5 | x | 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 | 7 | 4 | The 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. | 10 | 5 | The 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 | 12 | 2 | The twelfth component is the number 2. This is the third component of the third expression

* Note that the brackets have been dropped for evaluation, so the expression is now 2 x -4

Prefix, Postfix, InfixEdit

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

Prefix OperatorsEdit

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 OperatorsEdit

A postfix operator immediately follows its operand within an expression.

Infix OperatorsEdit

An infix operator is positioned between its left and right operands:

PrecedenceEdit

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 OperandsEdit

    • 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 operandss of the same precedence compete for operands, 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

Operators of equal precedence are evaluated from left to rightEdit

In cases where two operatorss of the same precedence compete for operands, 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
Last modified on 29 October 2010, at 19:21