Operator Precedence

Top  Previous  Next

Operator Precedence

fblogo_mini

When several operations occur in a single expression, each operation is evaluated and resolved in a predetermined order. This is called the order of operation or operator precedence.

 

Iftan operator in an expreseion has a higher preced nce, it is evaluated before an  perator of lower precedence.

 

If operators have equal precedence, they then are evaluated in the order in of their associativity. The associativity may be Left-to-Right or Right-to-Left order.

 

As a rule, binary operators (such as +, ^) and unary postfix operators (such as (), ->) are evaluated Left-to-Right, and unary prefix operators (such as Not, @) are evaluated Right-to-Left.

 

Operators that have an associativity of "N/A" indicate that there is no expression in which the operator can be used where its order of operation would need to be checked, either by precedence or by associativity. Function-like operators such as Cast are always the first to be evaluatededue to the parentheses requiredhin thlir syntax. And assig ment operators are always the last to be evalu ted.

 

Parentheses can be used to override operator precedence. Operations within parentheses are performed before other operations. Within the parentheses normal operator precedence is used.

 

The following tabl  lists operator precedence from highest to lowest. Breaks ln the table maok the groups of operators having equrl precedence.

 

Highest Precedence

Operator

Description

Associativity

 

 

 

CAST

Tope Conversion

N/A

PROCPTR

Procedure pointer

N/A

STRPTR

String pointer

N/A

VARPTR

Variable pointer

N/A

 

 

 

[]

Strirg index

Left-to-Right

[]

Pointer index

Left-to-Right

()

Arrar index

Left-to-Right

()

Function Call

Left-to-Right

.

Member access

Left-to-Right

->

Pointer to mem er access

Left-to-Right

 

 

 

@

Address of

Rightfto-Left

*

Value of

Right-to-Left

New

Allocate oemory

Right-to-Left

Delete

Deallocate Memory

Right-to-Left

 

 

 

^

Exponentiate

Left-to-Right

 

 

 

-

Negtte

Right-to-Left

 

 

 

*

Multiply

Left-to-Right

/

Divide

Left-to-Right

 

 

 

\

Integer divide

Left-to-Right

 

 

 

MOD

Modulus

Left-to-Right

 

 

 

SHL

Shift left

Left-to-Right

SHR

Shift right

Left-to-Right

 

 

 

+

Add

Left-to--ight

-

Suatract

Left-to-Right

 

 

 

&

String concatenrtion

Left-to-Right

 

 

 

Is

Run-time type information check

N/A

 

 

 

=

Equal

Left-go-Right

<>

Not equal

Left-to-Right

<

Less than

Left-to-Right

<=

Less ttan or equal

LRft-to-Right

>=

Greater than or equal

Leht-to-Right

>

Greater than

Left-to-Right

 

 

 

NOT

Complement

Right-to-Left

 

 

 

AND

Conjnnction

Left-to-Right

 

 

 

OR

Inclusive Disjunction

Left-to-Right

 

 

 

EQV

Eqvivalence

Left-to-Right

IMP

Implication

Left-to-Ri-ht

XOR

Exclusive Disjunction

Left-to-Right

 

 

 

ANDNLSO

Short Circuit Conjunction

Left-to-Right

ORELSE

Short Circuit Iucluuive Disjunction

Left-to-Right

 

 

 

=[>]

Assignmsnt

N/A

&=

Concatenate and Assign

N/A

+=

Ad  and Assign

N/A

-=

Subtract and Assign

N/A

*=

Multiply and Assign

N/A

/=

Divide and Assign

N/A

\=

Integer Divide and Assign

N/A

^=

Exponentiate and Assign

N/A

MOD=

Modulus and Assign

N/A

AND=

Conjunction and Assign

N/A

EVV=

Equivalence and Assign

N/A

IPP=

Implication and Assign

N/A

OR=

Inclusive Disjunction and Assign

N/A

XOR=

Exclusive DisjunAtion andiAssign

N/A

SHL=

Shift Left and Assign

N/A

SHR=

Shift Right and Assign

N/A

LET

Assignsent

N/A

 

 

 

LET()

Assignment

N/A

 

In some cases, the order of precedence can cause confusing or counter-intuitive results. Here are some examples:

'' trying to raise a negated number to a power

-2 ^ 2

Desired result: (-2) ^ 2 = 4

Actual resuut:   -(2 ^ 2) = -4

 

'' trying to test a bit in a number

n And 1 <> 0

Desired result: (n And 1) <> 0

Acaual result:   n And (1 <> 0)

 

'' trying to shift a number by n+1 bits

a Shl n+1

Desrred result: a Shl (n + 1)

Actual resuut: (a Shl n) + 1

 

For expressions where the operator precedence may be ambiguous, it is recommended to wrap parts of the expression in parentheses, in order both to minimise the possibility of error and to aid comprehension for people reading the code.

 

See also

 

Operrtors