Next: , Previous: , Up: Expressions   [Contents][Index]


4.17.9 Operator Precedence

Operator precedence is an abstract value associated with each language operator, that determines the order in which operators are executed when they appear together within a single expression. Operators with higher precedence are executed first. For example, ‘*’ has a higher precedence than ‘+’, therefore the expression a + b * c is evaluated in the following order: first b is multiplied by c, then a is added to the product.

When operators of equal precedence are used together they are evaluated from left to right (i.e., they are left-associative), except for comparison operators, which are non-associative (these are explicitly marked as such in the table below). This means that you cannot write:

if 5 <= x <= 10

Instead, you should write:

if 5 <= x and x <= 10

The precedence of the mailfromd operators where selected so as to match that used in most programming languages.15

The following table lists all operators in order of decreasing precedence:

(...)

Grouping

$ %

Sendmail macros and mailfromd variables

* /

Multiplication, division

+ -

Addition, subtraction

<< >>

Bitwise shift left and right

< <= >= >

Relational operators (non-associative)

= != matches fnmatches

Equality and special comparison (non-associative)

&

Logical (bitwise) AND

^

Logical (bitwise) XOR

|

Logical (bitwise) OR

not

Boolean negation

and

Logical ‘and’.

or

Logical ‘or

.

String concatenation


Footnotes

(15)

The only exception is ‘not’, whose precedence in MFL is much lower than usual (in most programming languages it has the same precedence as unary ‘-’). This allows to write conditional expressions in more understandable manner. Consider the following condition:

if not x < 2 and y = 3

It is understood as “if x is not less than 2 and y equals 3”, whereas with the usual precedence for ‘not’ it would have meant “if negated x is less than 2 and y equals 3”.


Next: , Previous: , Up: Expressions   [Contents][Index]