Precedence

Precedence is the set of rules imposed upon the evaluation of components of an expression that are not otherwise overridden by the presence of parentheses.

Based on the operation, expressions are evaluated from left-to-right (left-associative), from right-to-left (right-associative), or with no association (nonassociative). For example, multiplication and division are left-associative. The arithmetic expression below evaluates left-to-right:

x = 9 / 3 * 2

This first divides 9 by 3, yielding 3, which is then multiplied by 2, yielding the result 6. This expression evaluated as a right association returns the result 1.5.

Logical operations are nonassociative. For example, the expression below does not compile:

x = 1 < x < 3

Adding parentheses makes it a valid expression:

x = (1 < x) < 3

Adding the parentheses, however, does not return the results of (1 < x) and (x < 3).

x = (99 < 1) < 3

x = (0 < 1) < 3

The above two examples both evaluate as 1. In the first example, (99 < 1) evaluates as 0, and 0 < 3 evaluates as 1. In the second example, (0 < 1) evaluates as 1 and 1 < 3 evaluates as 1.

Below is a list of operations, precedence and associativity:

Symbol

Priority

Operation

Association

()

0 (high)

Parentheses

None

[]

1

Substring extraction

None

<>

1

Dynamic array reference

None

^

2

Exponentiation

Left

**

2

Exponentiation

Left

*

3

Multiplication

Left

/

3

Division

Left

\

3

Remainder

Left

+

4

Addition

Left

-

4

Subtraction

Left

+

4

Unary positive

Right

-

4

Unary negative

Right

mask

5

String masking

Left

cat

6

Concatenation

Right

:

6

Concatenation

Right

eq, =

7

Logical equal

None

ne, #

7

Not equal

None

<>

7

Not equal

None

lt, <

7

Less than

None

le, <=

7

Less than or =

None

gt, >

7

Greater than

None

ge, >=

7

Greater than or =

None

match

7

Pattern match

None

matches

7

Pattern match

None

and, &

8

Logical and

Left

or, !

8

Logical or

Left

Expressions are evaluated in order of precedence unless placed within parentheses. 10+2*10 is evaluated as 30. Expressions within the innermost parentheses are evaluated first. (10+2)*10 evaluates as 120.

See Also

() Reserved Characters

* Arithmetic Operator

*= Assignment Operator

+ Arithmetic Operator

+= Assignment Operator

- Arithmetic Operator

-= Assignment Operator

/= Assignment Operator

:= Assignment Operator

Arithmetic Expressions

Arithmetic Operators

Boolean Evaluation

Logical Expressions

Relational Operators

\= Assignment Operator