Operation Precedence

The precedence of operations determines the order in which operations are performed within expressions. High precedence operations are performed before lower precedence operations.

Since parentheses have the highest precedence, operations within parentheses are always performed first.

Operations of the same precedence (for example A+B+C) are evaluated in left to right order, except for **, which is evaluated from right to left.

(Note that although an expression is evaluated from left to right, this does not mean that the operands are also evaluated from left to right. See Order of Evaluation for additional considerations.)

The following list indicates the precedence of operations from highest to lowest:
  1. ()
  2. Built-in functions, user-defined functions
  3. unary +, unary -, NOT
  4. **
  5. *, /
  6. binary +, binary -
  7. =, <>, >, >=, <, <=, IN
  8. AND
  9. OR

Figure 1 shows how precedence works.

Figure 1. Precedence Example
 *..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
 * The following two operations produce different results although
 * the order of operands and operators is the same.  Assume that
 * PRICE = 100, DISCOUNT = 10, and TAXRATE = 0.15.
 * The first EVAL would result in a TAX of 98.5.
 * Since multiplication has a higher precedence than subtraction,
 * DISCOUNT * TAXRATE is the first operation performed.  The result
 * of that operation (1.5) is then subtracted from PRICE.

 /FREE
     TAX = PRICE - DISCOUNT * TAXRATE;
 
     // The second EVAL would result in a TAX of 13.50.
     // Since parentheses have the highest precedence the operation
     // within parenthesis is performed first and the result of that
     // operation (90) is then multiplied by TAXRATE.
 
     TAX = (PRICE - DISCOUNT) * TAXRATE;
 /END-FREE