Operator types



ColdFusion has Five types of operators:

  • Arithmetic

  • Boolean

  • Decision (or comparison)

  • String

  • Ternary

Functions also can be viewed as operators because they act on operands.

Arithmetic operators

The following table describes the arithmetic operators:

Operator

Description

+ - * /

Basic arithmetic: Addition, subtraction, multiplication, and division.

In division, the right operand cannot be zero.

++ --

Increment and decrement. Increase or decrease the variable by one.

These operators can be used for pre-incrementing or decrementing (as in x = ++ i), where the variable is changed before it is used in the expression. They can also be used for post-incrementing or decrementing (as in x = i++), where the value is changed after it is used in the expression. If the value of the variable i is initially 7, for example, the value of x in x = ++i is 8 after expression evaluation, but in x=i++, the value of x is 7. In both cases, the value of i becomes 8.

These operators cannot be used with expressions that involve functions, as in f().a++. Also, you can use an expression such as -++x, but ---x and +++x cause errors, because their meanings are ambiguous. You can use parentheses to group the operators, as in -(--x) or +(++x), however.

+= -= *= /= %=

Compound assignment operators. The variable on the right is used as both an element in the expression and the result variable. Thus, the expression a += b is equivalent to a = a +b.

An expression can have only one compound assignment operator.

+ -

Unary arithmetic: Set the sign of a number.

MOD

or %

Modulus: Return the remainder after a number is divided by a divisor. The result has the same sign as the divisor. The value to the right of the operator should be an integer; using a non-numeric value causes an error, and if you specify a real number, ColdFusion ignores the fractional part (for example, 11 MOD 4.7 is 3).

\

Integer division: Divide an integer by another integer. The result is also an integer; for example, 9\4 is 2. The right operand cannot be zero.

^

Exponentiation: Return the result of a number raised to a power (exponent). Use the caret character (^) to separate the number from the power; for example, 2^3 is 8. Real and negative numbers are allowed for both the base and the exponent. However, any expression that equates to an imaginary number, such -1^.5 results in the string "-1.#IND. ColdFusion does not support imaginary or complex numbers.

Boolean operators

Boolean, or logical, operators perform logical connective and negation operations. The operands of Boolean operators are Boolean (True/False) values. The following table describes the Boolean operators:

Operator

Description

NOT

or !

Reverse the value of an argument. For example, NOT True is False and the inverse.

AND

or &&

Return True if both arguments are True; return False otherwise. For example, True AND True is True, but True AND False is False.

OR

or ||

Return True if any of the arguments is True; return False otherwise. For example, True OR False is True, but False OR False is False.

XOR

Exclusive or: Return True if one of the values is True and the other is False. Return False if both arguments are True or both are False. For example, True XOR True is False, but True XOR False is True.

EQV

Equivalence: Return True if both operands are True or both are False. The EQV operator is the opposite of the XOR operator. For example, True EQV True is True, but True EQV False is False.

IMP

Implication: The statement A IMP B is the equivalent of the logical statement “If A Then B.” A IMP B is False only if A is True and B is False. It is True in all other cases.

Decision operators

The ColdFusion decision, or comparison, operators produce a Boolean True/False result. Many types of operation have multiple equivalent operator forms. For example, IS and EQ perform the same operation. The following table describes the decision operators:

Operator

Description

IS

EQUAL

EQ

Perform a case-insensitive comparison of two values. Return True if the values are identical.

IS NOT

NOT EQUAL

NEQ

Opposite of IS. Perform a case-insensitive comparison of two values. Return True if the values are not identical.

CONTAINS

Return True if the value on the left contains the value on the right.

DOES NOT CONTAIN

Opposite of CONTAINS. Return True if the value on the left does not contain the value on the right.

GREATER THAN

GT

Return True if the value on the left is greater than the value on the right.

LESS THAN

LT

Opposite of GREATER THAN. Return True if the value on the left is smaller than the value on the right.

GREATER THAN OR EQUAL TO

GTE

GE

Return True if the value on the left is greater than or equal to the value on the right.

LESS THAN OR EQUAL TO

LTE

LE

Return True if the value on the left is less than or equal to the value on the right.

Note: In CFScript expressions only, you can also use the following decision operators. You cannot use them in expressions in tags. == (EQ), != (NEQ), > (GT), < (LT), >= (GTE), and <= (LTE).

Decision operator rules

The following rules apply to decision operators:

  • When ColdFusion evaluates an expression that contains a decision operator other than CONTAINS or DOES NOT CONTAIN, it first determines if the data can be converted to numeric values. If they can be converted, it performs a numeric comparison on the data. If they cannot be converted, it performs a string comparison. This can sometimes result in unexpected results. For more information on this behavior, see Evaluation and type conversion issues.

  • When ColdFusion evaluates an expression with CONTAINS or DOES NOT CONTAIN it does a string comparison. The expression A CONTAINS B evaluates to True if B is a substring of A. Therefore an expression such as the following evaluates as True:

    123.45 CONTAINS 3.4
  • When a ColdFusion decision operator compares strings, it ignores the case. As a result, the following expression is True:

    "a" IS "A"
  • When a ColdFusion decision operator compares strings, it evaluates the strings from left to right, comparing the characters in each position according to their sorting order. The first position where the characters differ determines the relative values of the strings. As a result, the following expressions are True:

    "ab" LT "aba" 
    "abde" LT "ac"

String operators

String operators manipulate strings of characters. The following table describes the operators:

Operator

Description

&

Concatenates strings.

&=

Compound concatenation. The variable on the right is used as both an element in the concatenation operation and the result variable. Thus, the expression a &= b is equivalent to a = a & b.

An expression can have only one compound assignment operator.

Note: In a Query of Queries, you use || as the concatenation operator.

Ternary Operator

The ternary operator is a decision operator with three operands. It assigns a variable a value based on a Boolean expression. The operator has the form

(Boolean expression)? expression1 : expresson2

If the Boolean expression evaluates to true, the operator result is expression1; otherwise, it is expression2

For example

<cfset c = (a GT b)? a : b >

If a is greater than b, c is assigned the value of a; otherwise, c is assigned the value of b.

The parentheses can contain any expression that evaluates to a Boolean value, and a and b can be any valid expression. You can nest this operator inside other expressions.