Used with cfcase and cfdefaultcase. 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.
<cfswitch expression = "expression">
<cfcase value = "value" delimiters = "delimiters">
HTML and CFML tags
</cfcase>
additional <cfcase></cfcase> tags
<cfdefaultcase>
HTML and CFML tags
</cfdefaultcase>
</cfswitch>
cfabort, cfloop, cfbreak, cfrethrow, cfexecute, cfexit, cfthrow, cfif cfelseif cfelse, cftry cfcatch, cflocation
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.
<!--- 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>