cfswitch

Description

Evaluates a passed expression and passes control to the cfcase tag that matches the expression result. You can, optionally, code a cfdefaultcase tag, which receives control if there is no matching cfcase tag value.

Category

Flow-control tags

Syntax

<cfswitch 
  expression = "expression">
  <cfcase 
    value = "value" 
    delimiters = "delimiters">
    HTML and CFML tags
  </cfcase>
  additional <cfcase></cfcase> tags
  <cfdefaultcase>
    HTML and CFML tags
  </cfdefaultcase>
</cfswitch> 

See also

cfabort, cfloop, cfbreak, cfrethrow, cfexecute, cfexit, cfthrow, cfif, cftry, cflocation

History

New in ColdFusion MX: you can put the cfdefaultcase tag at any position within a cfswitch statement; it is not required to be the last item.

Attributes

Attribute Req/Opt Default Description
expression
Required
 
ColdFusion expression that yields a scalar value. ColdFusion converts integers, real numbers, Booleans, and dates to numeric values. For example, True, 1, and 1.0 are all equal.
value
Required
 
One or more constant values that cfswitch compares to the expression (case-insensitive). If a value matches expression, cfswitch executes the code between cfcase start and end tags.
Duplicate value attributes cause a runtime error.
delimiters
Optional
, (comma)
Character that separates entries in a list of values.

Usage

This tag requires an end tag. All code within this tag must be within a cfcase or cfdefaultcase tag. Otherwise, ColdFusion throws an error.

Use this tag followed by one or more cfcase tags. Optionally, include a cfdefaultcase tag. This tag selects the matching alternative from the cfcase and cfdefaultcase tags, jumps to the matching tag, and executes the code between the cfcase start and end tags. You do not have to explicitly break out of the cfcase tag, as you do in some languages.

You can specify only one cfdefaultcase tag within a cfswitch tag. You can put the cfdefaultcase tag at any position within a cfswitch statement; it is not required to be the last item. For example:

<cfset foo=1>
<cfswitch expression="#foo#">
  <cfdefaultcase>
    <cfset fooMore= #foo# + 1>
  </cfdefaultcase> 
  <cfcase value="2">
    <cfset fooMore = #foo# + 2>
  </cfcase>
</cfswitch> 

<cfdump var=#fooMore#> 

The cfswitch tag provides better performance than a series of cfif/cfelseif tags, and the code is easier to read.

Example

cfcase to 
exercise a case statement in CFML --->
<cfquery name = "GetEmployees" dataSource = "cfsnippets">
  SELECT  Emp_ID, FirstName, LastName, EMail, Phone, Department
  FROM   Employees
</cfquery>

<h3>cfswitch Example</h3>
<!--- By outputting the query and using cfswitch, we classify the 
output without using a cfloop construct. --->
<p>Each time the case is fulfilled, the specific information is printed; 
if the case is not fulfilled, the default case is output </p> 
<cfoutput query="GetEmployees"> 
<cfswitch expression="#Trim(Department)#"> 
  <cfcase value="Sales"> 
    #FirstName# #LastName# is in <b>sales</b><br><br> 
  </cfcase> 
  <cfcase value="Accounting"> 
    #FirstName# #LastName# is in <b>accounting</b><br><br> 
  </cfcase> <cfcase value="Administration"> 
    #FirstName# #LastName# is in <b>administration</b><br><br> 
  </cfcase> 
  <cfdefaultcase> 
    #FirstName# #LastName# is not in Sales, Accounting, or
      Administration.<br><br>
  </cfdefaultcase> 
</cfswitch> 
</cfoutput> 

Comments