User Guide Cancel

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

Syntax

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

See also

cfcasecfdefaultcasecfabortcfloopcfbreakcfexecutecfexitcfifcflocationcfrethrowcfthrowcftrycfswitchcfcase, and cfdefaultcase in the Developing ColdFusion Applications

History

ColdFusion 8: Changed the way the ColdFusion parses <cfcase> values. Previously, <cfcase> tags with numeric value dates did not return expected results{{. For example, <cfcase value="00">}} and <cfcase value="0A> were both evaluated to 0. The value "0A" was treated as a date and converted to 0 number of days from 12/30/1899. The value "00" was also evaluated to the value 0. This caused the exception "Context validation error for tag CFCASE. The CFSWITCH has a duplicate CFCASE for value "0.0"." The <cfswitch> tag now returns the expected result.

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

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.

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

 

<cfset language = "ColdFusion">
<cfswitch expression="#language#">
<cfcase value="ColdFusion">I like ColdFusion!</cfcase>
<cfcase value="Python;Julia" delimiters=";">I like Python!</cfcase>
<cfcase value="Java">I like Java!</cfcase>
<cfdefaultcase>Sorry, find your own language!</cfdefaultcase>
</cfswitch>
<cfset language = "ColdFusion"> <cfswitch expression="#language#"> <cfcase value="ColdFusion">I like ColdFusion!</cfcase> <cfcase value="Python;Julia" delimiters=";">I like Python!</cfcase> <cfcase value="Java">I like Java!</cfcase> <cfdefaultcase>Sorry, find your own language!</cfdefaultcase> </cfswitch>
<cfset language = "ColdFusion"> 
<cfswitch expression="#language#"> 
    <cfcase value="ColdFusion">I like ColdFusion!</cfcase>
    <cfcase value="Python;Julia" delimiters=";">I like Python!</cfcase> 
    <cfcase value="Java">I like Java!</cfcase>
    <cfdefaultcase>Sorry, find your own language!</cfdefaultcase> 
</cfswitch>

Get help faster and easier

New user?