Topics

Operator Precedence in ColdFusion

Wednesday, 5th March 2008 by Simon. Average Reading Time: about 2 minutes.

In arithmetic and algebra, when a number or expression is both preceded and followed by a binary operation (a calculation involving two operands), a rule is required for which operation should be applied first. From the earliest use of mathematical notation, multiplication took precedence over addition, whichever side of a number it appeared on. Thus 3 + 4 — 5 = 5 — 4 + 3 = 23.

Operator precedence, therefore, determines the order in which operators are evaluated in a statement. Operators with higher precedence are evaluated first.

A simple example can be expressed as follows:

<cfscript>x=3+4 * 5;writeOutput(x); //returns 23</cfscript>

This is equivalent to wrapping the 4 * 5 in parentheses:

<cfscript>x=3+(4 * 5);writeOutput(x); //returns 23</cfscript>

If we switched the parentheses, the result returned would be entirely different:

<cfscript>x=(3+4) * 5;writeOutput(x); //returns 35</cfscript>

This is an important point to note. Operators have an order of precedence, but this can be overridden using parentheses. Statements within parentheses are always evaluated first, before moving on to the outer statements.

Associativity

The concept of Associativity determines the order in which operators of the same precedence are processed. For example, consider the following expression (where OP stands for order of precedence):

a OP b OP c

Left-associativity (left-to-right) means that it is processed as:

(a OP b) OP c

Right-associativity (right-to-left) means it is interpreted as:

a OP (b OP c)

The following table details operators order of precedence:

Precedence

Operator type

Associativity

Individual operators

1

increment

n/a

++

decrement

n/a

—

2

logical-not

right-to-left

!

unary +

right-to-left

+

unary negation

right-to-left

–

3

multiplication

left-to-right

*

division

left-to-right

/

modulus

left-to-right

%

4

addition

left-to-right

+

subtraction

left-to-right

–

5

relational

left-to-right

<

<=

>

>=

6

equality

left-to-right

==

!=

7

logical-and

left-to-right

&&

8

logical-or

left-to-right

||

9

assignment

right-to-left

=

+=

-=

*=

/=

%=

&=

Summary

Warning: Multiplication and division are of equal precedence, and addition and subtraction are of equal precedence. Using any of the above rules in the order addition first, subtraction afterward would give the wrong answer to

10 - 3 + 2

The correct answer is 9, which is best understood by thinking of the problem as the sum of positive ten, negative three, and positive two.

10 + (-3) + 2

It is usual, wherever you need to calculate operations of equal precedence to work from left to right. The following rules of thumb are useful:

First: perform any calculations inside parentheses (brackets)

Second: Next perform all multiplication and division, working from left to right

Third: Lastly perform all addition and subtraction, working from left to right

Other articles I recommend

Following on from the first three parts of the “Using JavaScript Operators” in ColdFusion 8, where I introduced Arithmetic, Assignment and Logical Operators, it is now the turn of Comparison Operators.

An assignment operator assigns a value to its left operand based on the value of its right operand.

The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x. The other assignment operators are usually shorthand for standard operations, as shown in the following table.

An often mentioned complaint by ColdFusion developers is the lack of operators commonly found in other programming languages such as JavaScript. For example, instead of the greater-than (>) symbol we have been restricted to the more wordy GT or GREATER THAN operator. However, in ColdFusion 8 this has changed and we have more freedom to use familiar JavaScript operators in <cfscript> blocks.

In the following series of posts, I will introduce the changes and show some simple examples. The first in the series is Arithmetic Operators.

Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/).

I always use parentheses not only do I prefer explicit precedence, I believe it’s much easier to interpret the code to the human eye.

While “3 + 4 * 5” may behave like you intended, it’s too easy to accidentally interpret this as “(3 + 4) * 5” when you looking at the code. Explicitly writing the code as “3 + (4 * 5)” makes it very clear on the intend formula.

As a former Math major and after wading through years’ worth of someone else’s legacy code, I would love to make parentheses a required part of coding standards. I love that you used such a simple example to show what kind of errors can occur based on how one person reads vs. another (vs. a computer). The same can be said with some of the code I saw last year: