Operator Precedence

FreeBASIC

Operator Precedence
 
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.

If an operator in an expression has a higher precedence, it is evaluated before an operator 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 evaluated due to the parentheses required in their syntax. And assignment operators are always the last to be evaluated.

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 table lists operator precedence from highest to lowest. Breaks in the table mark the groups of operators having equal precedence.

Highest Precedence

OperatorDescriptionAssociativity
   
CASTType ConversionN/A
PROCPTRProcedure pointerN/A
STRPTRString pointerN/A
VARPTRVariable pointerN/A
   
[]String indexLeft-to-Right
[]Pointer indexLeft-to-Right
()Array indexLeft-to-Right
()Function CallLeft-to-Right
.Member accessLeft-to-Right
->Pointer to member accessLeft-to-Right
   
@Address ofRight-to-Left
*Value ofRight-to-Left
NewAllocate MemoryRight-to-Left
DeleteDeallocate MemoryRight-to-Left
   
^ExponentiateLeft-to-Right
   
-NegateRight-to-Left
   
*MultiplyLeft-to-Right
/DivideLeft-to-Right
   
\Integer divideLeft-to-Right
   
MODModulusLeft-to-Right
   
SHLShift leftLeft-to-Right
SHRShift rightLeft-to-Right
   
+AddLeft-to-Right
-SubtractLeft-to-Right
   
&String concatenationLeft-to-Right
   
IsRun-time type information checkN/A
   
=EqualLeft-to-Right
<>Not equalLeft-to-Right
<Less thanLeft-to-Right
<=Less than or equalLeft-to-Right
>=Greater than or equalLeft-to-Right
>Greater thanLeft-to-Right
   
NOTComplementRight-to-Left
   
ANDConjunctionLeft-to-Right
   
ORInclusive DisjunctionLeft-to-Right
   
EQVEquivalenceLeft-to-Right
IMPImplicationLeft-to-Right
XORExclusive DisjunctionLeft-to-Right
   
ANDALSOShort Circuit ConjunctionLeft-to-Right
ORELSEShort Circuit Inclusive DisjunctionLeft-to-Right
   
=[>]AssignmentN/A
&=Concatenate and AssignN/A
+=Add and AssignN/A
-=Subtract and AssignN/A
*=Multiply and AssignN/A
/=Divide and AssignN/A
\=Integer Divide and AssignN/A
^=Exponentiate and AssignN/A
MOD=Modulus and AssignN/A
AND=Conjunction and AssignN/A
EQV=Equivalence and AssignN/A
IMP=Implication and AssignN/A
OR=Inclusive Disjunction and AssignN/A
XOR=Exclusive Disjunction and AssignN/A
SHL=Shift Left and AssignN/A
SHR=Shift Right and AssignN/A
LETAssignmentN/A
   
LET()AssignmentN/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 result:   -(2 ^ 2) = -4

'' trying to test a bit in a number
n And 1  <>  0
Desired result: (n And 1) <> 0
Actual result:   n And (1 <> 0)

'' trying to shift a number by n+1 bits
a Shl n+1
Desired result: a Shl (n + 1)
Actual result: (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