Operator precedence and associativity – Teledyne LeCroy Merlins Wand - Users Manual User Manual

Page 278

Advertising
background image

262

CATC M

ERLIN

S

W

AND

2.00

C

HAPTER

D

User’s Manual

CATC Scripting Language

!1

which uses the logical negation operator. It returns a value of 0.

The unary operators are sizeof(), head(), tail(), ~ and !.

Operator Precedence and Associativity

Operator rules of precedence and associativity determine in what order
operands are evaluated in expressions. Expressions with operators of higher
precedence are evaluated first. In the expression

4 + 9 * 5

the * operator has the highest precedence, so the multiplication is performed
before the addition. Therefore, the expression evaluates to 49.

The associative operator () is used to group parts of the expression, forcing
those parts to be evaluated first. In this way, the rules of precedence can be
overridden. For example,

( 4 + 9 ) * 5

causes the addition to be performed before the multiplication, resulting in a
value of 65.

When operators of equal precedence occur in an expression, the operands
are evaluated according to the associativity of the operators. This means that
if an operator's associativity is left to right, then the operations will be done
starting from the left side of the expression. So, the expression

4 + 9 - 6 + 5

would evaluate to 12. However, if the associative operator is used to group
a part or parts of the expression, those parts are evaluated first. Therefore,

( 4 + 9 ) - ( 6 + 5 )

has a value of 2.

In the following table, the operators are listed in order of precedence, from
highest to lowest. Operators on the same line have equal precedence, and
their associativity is shown in the second column.

Operator Symbol

Associativity

[]

()

Left to right

~

!

sizeof

head

tail

Right to left

*

/

%

Left to right

Advertising