Decision Operators in ColdFusion 8

ColdFusion 8 introduced a few new decision operators.

== (is the same as EQ)

!= (is the same as NEQ)

> (is the same as GT)

< (is the same as LT)

>= (is the same as GTE)

<= (is the same as LTE)

Note that these are available only when used in expressions inside a CFScript block. And they are NOT available in expressions inside CF tags.

<cfscript>
iNum1 = 10;
iNum2 = 20;

writeOutput("<p>iNum1 = #iNum1#<br/>iNum2 = #iNum2#</p>");

/* this is same as using the EQ operator */
bResult = (iNum1 == iNum2);
writeOutput("<p>Is iNum1 equal to iNum2 (iNum1 == iNum2): #bResult#</p>");

/* this is same as using the NEQ operator */
bResult = (iNum1 != iNum2);
writeOutput("<p>Is iNum1 not equal to iNum2 (iNum1 != iNum2): #bResult#</p>");

/* this is same as using the GT operator */
bResult = (iNum1 > iNum2);
writeOutput("<p>Is iNum1 greater than iNum2 (iNum1 > iNum2): #bResult#</p>");

/* this is same as using the LT operator */
bResult = (iNum1 < iNum2);
writeOutput("<p>Is iNum1 less than iNum2 (iNum1 < iNum2): #bResult#</p>");

/* this is same as using the GTE operator */
bResult = (iNum1 >= iNum2);
writeOutput("<p>Is iNum1 greater than or equal to iNum1: (iNum1 >= iNum2): #bResult#</p>");

/* this is same as using the LTE operator */
bResult = (iNum1 <= iNum2);

writeOutput("<p>Is iNum1 less than or equal to iNum1: (iNum1 >= iNum2): #bResult#</p>");
</cfscript>

The code above produces this output.

=======Start output=======

iNum1 = 10
iNum2 = 20

Is iNum1 equal to iNum2 (iNum1 == iNum2): NO

Is iNum1 not equal to iNum2 (iNum1 != iNum2): YES

Is iNum1 greater than iNum2 (iNum1 > iNum2): NO

Is iNum1 less than iNum2 (iNum1 < iNum2): YES

Is iNum1 greater than or equal to iNum1: (iNum1 >= iNum2): NO

Is iNum1 less than or equal to iNum1: (iNum1 >= iNum2): YES

=======End output=======

More information on these can be found in the Adobe Livedocs. Note: Please also have a look at the discussion comments at the bottom of the the related blog entry Boolean Operators in ColdFusion 8.

Comment on this blog post...