Conditional Processing

Contact Us or call 1-877-932-8228
Conditional Processing

Conditional Processing

Conditional processing allows programmers to output different code based on specific conditions. There are two conditional structures in ColdFusion <cfif> blocks and <cfswitch> blocks.

If-Else if-Else Blocks

Simple <cfif>

The syntax for a simple <cfif> condition is shown below:

<cfif conditions> Hello World! </cfif>

In the above code, Hello World will be displayed only if the conditions are true.

<cfif><cfelse></cfif>

The syntax for a simple <cfif><cfelse> condition is shown below:

<cfif conditions> Hello World! <cfelse> Goodbye World! </cfif>

<cfif><cfelseif><cfelse></cfif>

The syntax for a simple <cfif><cfelse><cfelseif> condition is shown below:

<cfif conditions> Hello World! <cfelseif conditions> World, are you there? <cfelse> Goodbye World! </cfif>

There can be an unlimited number of <cfelseif> blocks, but a maximum of one <cfelse> block.

Switch Blocks

A <cfswitch> statement is similar to a <cfif> statement, except that it can only check for an equality comparison of a single expression. It cannot, for example, be used to check if one value is higher than another.

<cfswitch expression="#language#"> <cfcase value="German"> Hallo </cfcase> <cfcase value="Spanish"> Hola </cfcase> <cfcase value="French"> Bonjour </cfcase> <cfdefaultcase> Hello </cfdefaultcase> </cfswitch>

Decision and Boolean Operators

ColdFusion use words rather than symbols for its decision and boolean operators.

Operator Type Operator Description
Decision
IS
EQ
EQUAL
Returns True if the values are equal.
IS NOT
NEQ
NOT EQUAL
Returns True if the values are not equal.
GREATER THAN
GT
Return True if the value on the left is greater than the value on the right.
LESS THAN
LT
Return True if the value on the right is greater than the value on the left.
GREATER THAN OR EQUAL TO
GTE
GE
Returns 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
Returns True if the value on the right is greater than or equal to the value on the left.
CONTAINS
Returns True if the value on the left contains the value on the right.
DOES NOT CONTAIN
Returns True if the value on the left does not contain the value on the right.
Boolean
NOT
Reverses the value of an argument.
AND
Returns True if both arguments are True.
OR
Returns True if either argument is True.

Short-circuiting

ColdFusion stops evaluating a Boolean condition as soon as it knows the result. For example, when checking to see if "a AND b" is true, ColdFusion will only look at b if a is true, because if a is false then the whole expression is also false, regardless of the value of b. Likewise, when checking to see if "a OR b" is true, ColdFusion will only look at b if a is false, because if a is true then the whole expression is also true, regardless of the value of b. This behavior is called short-circuiting.

Condition Examples

Following are some examples of if-else if-else and switch-case expressions.

Code Sample:

FlowControl/Demos/If.cfm
<html>
<head>
<title>If-Else If-Else</title>
</head>
<body>
<cfset Age = 21>
<cfif Age GTE 21>
	You can vote and drink!
<cfelseif Age GTE 18>
	You can vote, but can't drink.
<cfelse>
	You cannot vote or drink.
</cfif>
</body>
</html>

The file is relatively simple. You can see the different results by changing the value of Age.

Code Sample:

FlowControl/Demos/If2.cfm
<html>
<head>
<title>If-Else If-Else - Compound</title>
</head>
<body>
<cfset Age = 21>
<cfset Citizen = false>
<cfif Age GTE 21 AND NOT Citizen>
	You can drink but you can't vote!
<cfelseif Age GTE 21>
	You can vote and drink!
<cfelseif Age GTE 18 AND Citizen>
	You can vote but can't drink!
<cfelse>
	You cannot vote or drink.
</cfif>
</body>
</html>

The example above shows the use of compound if conditions using boolean operators.

Code Sample:

FlowControl/Demos/Switch.cfm
<html>
<head>
<title>Switch-Case</title>
</head>
<body>
<cfset Quantity = 1>
<cfswitch expression="#Quantity#">
	<cfcase value="1">
		Quantity is 1
	</cfcase>
	<cfcase value="2">
		Quantity is 2
	</cfcase>
	<cfdefaultcase>
		Quantity is not 1 or 2
	</cfdefaultcase>
</cfswitch>
</body>
</html>

In many languages, a break is needed to end switch-case processing. In ColdFusion, this is not the case. As soon as the expression is matched, that case block code is executed, and the switch-case block exits.

Next