Programmer Guide > Expressions and Operators > Operator Precedence
  

Operator Precedence
Operators are divided into the levels of algebraic precedence found in common arithmetic. Operators with higher precedence are evaluated before those with lesser precedence, and operators of equal precedence are evaluated from left to right. Operators are grouped into five classes of precedence as shown in the following table:
 
Table 3-1: Operator Precedence  
Priority
Operator
First (highest)
^ (exponentiation)
Second
* (multiplication)
# (matrix multiplication)
/ (division)
MOD (modulus)
Third
+ (addition)
(subtraction)
< (minimum)
> (maximum)
NOT (Boolean negation)
Fourth
EQ (equality)
NE (not equal)
LE (less than or equal)
LT (less than)
GE (greater than or equal)
GT (greater than)
Fifth
AND (Boolean AND)
OR (Boolean OR)
XOR (Boolean exclusive OR)
For example, the expression:
 4 + 5 * 2 
yields 14 because the multiplication operator has a higher precedence than the addition operator. Parentheses may be used to override the default evaluation:
(4 + 5) * 2
yields 18 because the expression inside the parentheses is evaluated first. A useful rule of thumb is when in doubt, parenthesize. Some examples of expressions are:
; The value of variable A.
A
; The value of A plus 1.
A + 1
; The smaller of A or 2, plus 1.
A < 2 + 1
; The smaller of A and 6; The multiplication operator (*) has a
; higher precedence than the minimum operator (<).
A < 2 * 3
; Twice the square-root of A.
2 * SQRT(A)
; The concatenation of the strings A and 'Thursday'. An error 
; will result if A is not a string.
A + 'Thursday'

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.