docs.roxen.comView this page in a printer friendly mode
DocsPike7.0TutorialExpressions
Copyright © 2012, Roxen Internet Software
Suggestions, comments & compliments
manuals@roxen.com
 DEMO  DOCS  PIKE
 COMMUNITY  DOWNLOAD
www.roxen.com

   

Some Terminology
Arithmetical Operations
Operations on Complex Types
Comparison
Logical Expressions
Bitwise Operations
Operations on Sets
Indexing
Assignment
Type Conversions
The Comma Operator
Call and Splice
Operator Precedence and Associativity

Operator Precedence and Associativity

Operator precedence is why the expression 5 + 3 * 2 is calculated as 5 + (3 * 2), giving 11, and not as (5 + 3) * 2, giving 16.

We say that the multiplication operator (*) has higher "precedence" or "priority" than the addition operator (+), so the multiplication must be performed first.

Operator associativity is why the expression 8 - 3 - 2 is calculated as (8 - 3) - 2, giving 3, and and not as 8 - (3 - 2), giving 7.

We say that the subtraction operator (-) is "left associative", so the left subtraction must be performed first. When we can't decide by operator precedence alone in which order to calculate an expression, we must use associativity.

Since the operators + and - have the same precedence, and are left-associative, the following expressions are all equivalent:

x + 3 - y + 5
(x + 3) - y + 5
((x + 3) - y) + 5
((x + 3) - y + 5)

Since the operators =, += and -= have the same precedence, and are right-associative, the following expressions are all equivalent:

x = y += z -= 4
x = y += (z -= 4)
x = (y += (z -= 4))
(x = y += (z -= 4))

You can use parentheses to tell the compiler in which order to evaluate things. If you don't use parentheses, Pike will use the precedences and the associativities of the operators to decide in which order to perform them. Spaces have no effect.

This table shows the priority and associativity of each operator in Pike, with the highest priority at the top:

OperatorsAssociativity

::a a::b

left to right

a() a[b] a->b a[b..c] ({}) ([]) (<>)

left to right

a++ a--

left to right

!a ~a (type)a ++a --a

right to left!

a*b a/b a%b

left to right

a+b a-b

left to right

a>>b a<<b

left to right

a>b a>=b a<b a<=b

left to right

a==b a!=b

left to right

a&b

left to right

a^b

left to right

a|b

left to right

&&

left to right

||

left to right

a?b:c

right to left!

=
+=
-=
*=
/=
%=
<<=
>>=
&=
|=
^=

right to left!

@a

right to left!

,

left to right

Examples:

This expression is evaluated in this order

1+2*2

1+(2*2)

1+2*2*4

1+((2*2)*4)

(1+2)*2*4

((1+2)*2)*4

1+4,c=2|3+5

(1+4),(c=(2|(3+5)))

1 + 5&4 == 3

(1 + 5) & (4 == 3)

c=1,99

(c=1),99

!a++ + ~f()

(!(a++)) + (~(f()))

s == "klas" || i < 9

(s == "klas") || (i < 9)

r = s == "sten"

r = (s == "sten")