<cfcase>

edit

Handle one case of a <cfswitch>

This tag requires a body/content and a closing tag.

This tag is also supported within <cfscript>

<cfcase value=string delimiters=string ><!--- body ---></cfcase>
Attribute Description
value
string, required
edit

One or more constant values that cfswitch compares to the specified expression case-insensitive comparison.

If a value matches the expression, cfswitch executes the code between the cfcase start and end tags.

Separate multiple values with a comma or other delimiter, as specified in the delimiters parameter.

Duplicate value attributes are not allowed and causes a runtime error.

delimiters
string, optional
edit

Specifies the character that separates multiple entries in a list of values. You can specify multiple delimiter characters and you can use any of them to separate the values to be matched. Delimiters can only be used in the tag syntax.

Default delimiter: ,

Note: in <cfscript>, delimitered values are not supported for case statements. Use an individual case statement for each value.

Usage Notes

edit

<cfcase> does not fall through to the next case. When a case body finishes, the <cfswitch> exits — there is no implicit fall-through and no explicit break is needed.

A <cfbreak> written inside a <cfcase> is therefore redundant for ending the case, and is a common footgun: if the <cfswitch> sits inside a <cfloop> or <cfwhile>, the <cfbreak> will exit that enclosing loop instead. See <cfbreak> for details.

This differs from script case, which follows C-style fall-through semantics where break; is required to stop after a match.

Examples

edit

Tag syntax

<cfset expr = 2>
<cfswitch expression="#expr#">
    <cfcase value=1>
        this is from case 1
    </cfcase>
    <cfcase value=2$3$4 delimiters="$">
        this is from case 2
    </cfcase>
    <cfcase value=5,6,7>
        this is from case 3
    </cfcase>
    <cfdefaultcase>
        this is from default part
    </cfdefaultcase>
</cfswitch>

Script syntax

  expr = 1;
    switch(expr){
        case 1:
            result = 1;
            break;
        case 2:
        case 3:
            result = "2 or 3";
            break;
        case 0:
            result = 0;
            break;
        default:
            result = "defaultCase";
    }
    dump( result );

See also