Conditional Processing (CFIF and CFSWITCH)  
 
 

ColdFusion offers two ways to handle conditional processing: CFSWITCH and CFIF. These tags allow you to customize the behavior of your ColdFusion applications in powerful ways.

 
 
  Using CFSWITCH with CFCASE and CFDEFAULTCASE  
 
 

Used with CFCASE and CFDEFAULTCASE, the CFSWITCH tag evaluates a passed expression and passes control to the CFCASE tag that matches the expression result. You can also optionally include a CFDEFAULTCASE, which receives control if there is no matching CFCASE tag value.

The CFSWITCH tag offers better performance than a series of CFIF/CFELSEIF tags, and the resulting code is easier to read. However, the VALUE attributes of CFCASE tags must be constants whose value is known at when server processes the page.

The following example shows the syntax of a simple CFSWITCH tag:

<CFSWITCH EXPRESSION=#Switch#>
    <CFCASE VALUE="4"> Case four </CFCASE>
    <CFCASE VALUE="1"> Case one </CFCASE>
    <CFCASE VALUE="2"> Case two </CFCASE>
    <CFCASE VALUE="3"> Case three </CFCASE>
    <CFCASE VALUE="2.5"> Case two and a half </CFCASE>
    <CFCASE VALUE="5"> Case five </CFCASE>
    <CFCASE VALUE="6"> Case six </CFCASE>

<CFDEFAULTCASE> Default case </CFDEFAULTCASE>

</CFSWITCH>

In each CFCASE tag, the VALUE attribute shows one or more constant values that CFSWITCH compares to the specified expression (in this example, the variable #Switch#). If a value matches the expression, CFSWITCH executes the code between the CFCASE start and end tags.

See the CFML Language Reference for details on the syntax of CFSWITCH.

 
 
  Using CFIF with CFELSEIF and CFELSE  
 
 

You can also use CFIF, CFELSE, and CFELSEIF to conditionally process a section of an application page.

The specific syntax for a simple conditional block is:

<CFIF value operator value>
... HTML and CFML tags
<CFELSE>
... HTML and CFML tags
</CFIF>

The CFELSE tag is not required.

Note that the expression inside the CFIF tag uses "operators" such as IS, IS NOT, etc. rather than equal signs. See the Functions and Expressions chapter in Advanced ColdFusion Development for information on conditional operators in ColdFusion.

 
 
  Example 1: Conditionally returning a query result set  
 

To test whether a query returned any records, you can check wither the query's record count is zero, using the syntax:

<CFIF #CustomerSearch.RecordCount# IS 0>

<!--- Inform user that we had no hits --->
<P>Sorry, no customers matching your 
criteria were found.</P>

<CFELSE>
<!--- Show the list of customers retrieved --->
    <CFOUTPUT Query="Customers">
    #FirstName# #LastName# <BR>
    </CFOUTPUT>
</CFIF>

To display an output section only if the user explicitly requests it, as recorded in this example in a variable called ShowCustomers, use the syntax:

<CFIF #Form.ShowCustomers# IS "Yes">
    Customer List: <P>
    <CFOUTPUT Query="Customers">
    #FirstName# #LastName# <BR>
    </CFOUTPUT>
</CFIF>
 
 
  Example 2: Conditionally returning a record section  
 

One of the most powerful uses of conditional tags is for record-by-record formatting of query results. To accomplish this, place the conditional tags within CFOUTPUT sections.

When conditional tags are placed within CFOUTPUT sections , they are evaluated once for every row in the query result set. This allows you to customize the display of results depending upon whether or not a field is present in an individual row.

For example, in this example, not every contact in the result set has a phone number. So putting the CFIF inside CFOUTPUT ensures that the "Phone:" label is printed only where the #Phone# field has a value. The label is not printed if the #Phone# variable is empty.

<CFOUTPUT QUERY="Contacts">

    <HR>
    Name: #Name# <BR>
    Title: #Title# <BR>
<CFIF #Phone# IS NOT "">
    Phone: #Phone# <BR>
</CFIF>
</CFOUTPUT>
 
 
  Compound conditional statements  
 
 

A compound conditional statement combines multiple conditional statements with Boolean operators. The specific syntax for a compound conditional block is:

<CFIF (value operator value) Boolean Operator
    (value operator value) Boolean Operator
    (value operator value)>

... HTML and CFML tags
</CFIF>

You can use CFELSE and CFELSEIF with compound conditional blocks. The most common Boolean operators are:

Operator
Description
AND
Conjunction
OR
Disjunction
NOT
Logical negation

 
 
  Example  
 

The following example assumes that a query named "GetEmployee" has returned information about an employee. The code displays an HTML message if the query result for an employee file shows that the employee is both in the sales department and earns a bonus of more than $5,000 a year.

<CFIF (#GetEmployee.Department# IS "Sales") AND
    (#GetEmployee.Bonus# GE 5000)>

<H4>Congratulations on your sales bonus!</H4>
</CFIF>
 
 
  Using CFELSEIF  
 
 

Using CFELESIF allows you to perform sophisticated conditional processing within your dynamic pages. It offers a way to combine conditional statements without multiple nested IF statements. The following example shows the syntax of a conditional block.

<CFIF condition1>
    Display this text only if condition1 is true.

<CFELSEIF condition2>
    Display this text only if condition1 is
    false and condition2 is true.

<CFELSEIF condition3>
    Display this text only if condition1 
    and condition2 are false and condition3 is true.

<CFELSE>
    Display this if condition1, condition2, 
    and condition3 are false.
</CFIF>
 
 
  Note  
 

CFELSEIF can only be used within a CFIF. The CFELSE is optional.

 
 
  Example  
 

Imagine an application in which you enter your age and then see a message based on that age. The following conditional block would display the proper message depending on the age you entered.

<CFIF #Form.Age# LESS THAN 12>
You're only a child (0 - 11 years old).

<CFELSEIF #Form.Age# LESS THAN 20>
You're a teenager (12 - 19 years old).

<CFELSEIF #Form.Age# LESS THAN 60>
You're an adult (20 - 59 years old).

<CFELSE>
You're a senior citizen (older than 59).
</CFIF>


 
 
BackUp LevelNext
 
 

allaire     AllaireDoc@allaire.com
    Copyright © 1998, Allaire Corporation. All rights reserved.