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. Note the difference in the tag and script syntax when providing multiple values for a case.

 <cfswitch expression="">

Attribute Reference for the cfswitch tag


Required: Yes
CFML expression that yields a scalar value. CFML converts integers, real numbers, Booleans, and dates to numeric values. For example, True, 1, and 1.0 are all equal.

Examples sample code using the cfswitch tag

Script Syntax

fruit = "Orange";
switch(fruit) {
    case "Apple":
        writeOutput("I like apples!");
    case "Orange": case "Citrus":
         writeOutput("I like oranges!");
    case "Kiwi":
        writeOutput("I like kiwi!"); 
        writeOutput("Fruit, what fruit?"); 

Expected Result: I like oranges!

Tag Syntax

<cfset fruit = ""> 
<cfswitch expression="#fruit#"> 
    <cfcase value="Apple">I like apples!</cfcase>
    <cfcase value="Orange,Citrus">I like oranges!</cfcase> 
    <cfcase value="Kiwi">I like kiwi!</cfcase>
    <cfdefaultcase>Fruit, what fruit?</cfdefaultcase> 

Expected Result: Fruit, what fruit?

Fork me on GitHub