View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cfswitch PreviousNext

cfswitch

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.

Flow-control tags

<cfswitch 
expression = "expression">
one or more cfcase tags
zero or one cfdefaultcase tags </cfswitch>

cfcase, cfdefaultcase
also cfabort, cfloop, cfbreak, cfrethrow, cfexecute, cfexit, cfthrow, cfif, cftry, cflocation

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.

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.

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.

<!--- 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> <!--- 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>

Contents > CFML Reference > ColdFusion Tags > cfswitch PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


bsimmons said on May 26, 2004 at 8:10 AM :
This page states:
"All code within this tag must be within a cfcase or cfdefaultcase tag. Otherwise, ColdFusion throws an error."
This statement is not true (at least in CF MX 6.1), the following code runs just fine:
<CFSET x=1>
<CFSWITCH EXPRESSION="#x#">
<CFCASE VALUE="0">test1</CFCASE>
not in case 1
<CFCASE VALUE="2">test2</CFCASE>
not in case 2
</CFSWITCH>
ASandstrom said on May 26, 2004 at 9:00 AM :
Your example doesn't throw an error, but there's no CFML code outside of the CFCASE tags. If you were to alter your code as in the following example, you would see that ColdFusion throws an error when it encounters the CFOUTPUT.
<CFSET x=1>
<CFSWITCH EXPRESSION="#x#">
<CFCASE VALUE="0">test1</CFCASE>
<CFOUTPUT>The value of x is #x#.</CFOUTPUT>
<CFCASE VALUE="2">test2</CFCASE>
not in case 2
</CFSWITCH>
MikerRoo said on Dec 29, 2004 at 8:28 PM :
Switch and Case take strings the documentation should explicitely say whether these tags are case-sensitive.

Also, the doc says that the expression should evaulate to a scalar. Is a string normally considered a scalar value?
KerryKing said on Jul 11, 2005 at 4:23 AM :
switch thinks "00" and "0A" are both "0.0"....

function Hex7toStr(Char){
switch (Char)
{
case "00":
newchar = "NUL";
break;
case "0A":
newchar = "LF";
break;
}
return newchar;
}

Is this expected behaviour?

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-c10.htm