Boolean operators in ColdFusion 8: NOT, AND, OR

In ColdFusion 8 one can use shortcuts to refer to three boolean operators: NOT, AND, OR

NOT can also be used to as !

AND can also be used as &&

OR can also be used as ||

<cfset x = 10>
<cfset y = 30>

<cfoutput>
x = #x#<br/>
y = #y#
</cfoutput>

<!--- Reverse the value of an argument. For example, NOT True is False and vice versa. --->
<p><strong>Using NOT or !:</strong> Testing for !x eq y </p>
<cfif !x eq y>
x is not equal to y
<cfelse>
x is equal to y
</cfif>

<p><strong>Using AND or &&</strong>: Testing for x eq 10 && y eq 30</p>
<!--- Return True if both arguments are True; return False otherwise. For example, True AND True is True, but True AND False is False. --->
<cfif x eq 10 && y eq 30>
x is equal to y
<cfelse>
x is not equal to y
</cfif>

<p><strong>Using OR or ||</strong>: Testing for x eq 10 || y eq 20</p>
<!--- 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. --->
<cfif x eq 10 || y eq 20>
One of the values is true
<cfelse>
None of the values are true
</cfif>

This code yields the following output.

=======Start output=======
x = 10
y = 30

Using NOT or !: Testing for !x eq y

x is not equal to y

Using AND or &&: Testing for x eq 10 && y eq 30

x is equal to y

Using OR or ||: Testing for x eq 10 || y eq 20

One of the values is true

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

The other boolean operators — XOR, EQV, IMP — continue to work as before.
More information on boolean operators in ColdFusion 8 is at Adobe ColdFusion 8 Livedocs.

1 thought on “Boolean operators in ColdFusion 8: NOT, AND, OR”

  1. It looks kind of strange because you use them both.
    <cfif x eq 10 && y eq 30>
    x is equal to y
    <cfelse>
    x is not equal to y
    </cfif>
    Why not use:
    <cfif x == 10 && y == 30>
    x is equal to y
    <cfelse>
    x is not equal to y
    </cfif>
    and:
    <cfif x != y>
    x is not equal to y
    <cfelse>
    x is equal to y
    </cfif>

Comment on this blog post...