CFSWITCH CFCASE 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 optionally code a CFDEFAULTCASE tag, which receives control if there is no matching CFCASE tag value.

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>

EXPRESSION

Required. Any 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 specified expression (case-insensitive comparison). If a value matches the expression, CFSWITCH executes the code between the CFCASE start and end tags.

Separate multiple values with a comma or an alternative delimiter, as specified in the DELIMITERS parameter. Duplicate value attributes are not allowed and will cause a runtime error.

DELIMITERS

Optional. Specifies the character that separates multiple entries in a list of values. The default delimiter is the comma (,).

Usage

Use CFSWITCH followed by one or more CFCASE tags, optionally ending with a CFDEFAULTCASE tag. The CFSWITCH tag selects the matching alternative from the specified CFCASE and CFDEFAULTCASE tags and jumps to the matching tag, executing the code between the CFCASE start and end tags. There is no need to explicitly break out of the CFCASE tag, as there is in some other languages.

You can specify only one CFDEFAULTCASE tag within a CFSWITCH tag. CFCASE tags cannot appear after the CFDEFAULTCASE tag.

CFSWITCH provides better performance than a series of CFIF/CFELSEIF tags and the resulting code is easier to read.

Example

<!--- This example illustrates the use of CFSWITCH and
CFCASE to exercise a case statement in CFML --->

<!--- query to get some information --->
<CFQUERY NAME="GetEmployees" DATASOURCE="cfsnippets">
SELECT   Emp_ID, FirstName, LastName, EMail,
         Phone, Department
FROM     Employees
</CFQUERY>

<HTML>
<HEAD>
<TITLE>
CFSWITCH Example
</TITLE>
</HEAD>

<BODY bgcolor=silver>
<H3>CFSWITCH Example</H3>

<!--- By outputting the query and using CFSWITCH,
we can classify the output without using a CFLOOP construct.
 --->
<CFOUTPUT QUERY="GetEmployees">
<CFSWITCH EXPRESSION=#Department#>
<!--- each time the case is fulfilled, the specific
information is printed; if the case is not fulfilled,
the default case is output --->
    <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>
    </CFDEFAULTCASE>
</CFSWITCH>
</CFOUTPUT>

</BODY>
</HTML>