User Guide
330 Chapter 2: ColdFusion Tags
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">
one or more cfcase tags
zero or one cfdefaultcase tags
</cfswitch>
See also
cfcase, cfdefaultcase
also cfabort, cfloop, cfbreak, cfrethrow, cfexecute, cfexit, cfthrow, cfif,
cftry, cflocation
History
ColdFusion MX: Changed cfdefaultcase tag placement requirements: you can put the
cfdefaultcase tag at any position within a cfswitch statement; it is not required to be the last
item.
Attributes
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.
The cfswitch tag provides better performance than a series of cfif/cfelseif tags, and the code
is easier to read.
Example
<!--- This example shows the use of cfswitch and 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>
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.