<cfswitch>
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.
This tag requires a body/content and a closing tag.
This tag is also supported within <cfscript>
<cfswitch
expression=string
><!--- body ---></cfswitch>
| Attribute | Description |
|---|---|
expression
string, required
|
edit
Any CFML expression that yields a scalar value. CFML converts integers, real numbers, Booleans, and dates to numeric values. |
Usage Notes
edit<cfcase> blocks do not fall through. Once a matching case body finishes, the switch exits — there is no implicit fall-through to the next case and no need for an explicit break.
This differs from script switch/case, which follows C-style fall-through semantics where break; is required to stop after a match.
A consequence: <cfbreak> placed inside a <cfcase> does not "break the switch" (the switch is already exiting). If the switch sits inside a <cfloop> or <cfwhile>, the <cfbreak> will exit that enclosing loop instead. See <cfbreak> for details.
Examples
edit<cfoutput>
<cfset expr = 2>
<cfswitch expression="#expr#">
<cfcase value=1>
this is from case 2
</cfcase>
<cfcase value=2$3$4 delimiters="$">
this is from case 2
</cfcase>
<cfdefaultcase>
this is from default part
</cfdefaultcase>
</cfswitch>
<cfscript>
//Script example
switch(1){
case 1:
result = 1;
break;
case 0:
result = 0;
break;
default:
result = "defaultCase";
}
writeDump(result);
</cfscript>
</cfoutput>
See also
- Core CFML Language
- Flow Control
- <cfcase>
- <cfdefaultcase>
- Search Issue Tracker open_in_new
- Search Lucee Test Cases open_in_new (good for further, detailed examples)