Simon Baynes: Web Development

cfscript insight part (2)

As promised I am revisiting cfscript to go over some more of its functionality.

switch statements
Now I assume most of you know how to use cfswitch, for those of you that don't it is a flow-control tag that allows you to make decisions based on the value of a variable.

OK here is a switch statement using basic cfml tags.

<cfswitch expression="#FlowerColour#">
    <cfcase value="Purple">
        <cfset FlowerSuggestion = "Violets">
    </cfcase>

    <cfcase value="Yellow">
        <cfset FlowerSuggestion = "Daffodils">
    </cfcase>

    <cfcase value="Red;White" delimiter=";">
        <cfset FlowerSuggestion = "Roses">
    </cfcase>

    <cfdefaultcase>
        <cfset FlowerSuggestion = "No suggestions">
    </cfdefaultcase>
</cfswitch>

Now here is the same statement in cfscript:-

<cfscript>
        switch (FlowerColour) {
             case "Purple":
                 FlowerSuggestion = "Violets";
             break;

             case "Yellow":
                 FlowerSuggestion = "Daffodils";
             break;

             case "Red": case "White":
                 FlowerSuggestion = "Roses";
             break;

             default:
                 FlowerSuggestion = "No suggestions";  
             break;
         }
</cfscript>

The things to really keep an eye on are making sure that you add a break at the end of each case statement otherwise it will throw an error.

By Simon Baynes