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 value="Yellow">
        <cfset FlowerSuggestion = "Daffodils">

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

        <cfset FlowerSuggestion = "No suggestions">

Now here is the same statement in cfscript:-

        switch (FlowerColour) {
             case "Purple":
                 FlowerSuggestion = "Violets";

             case "Yellow":
                 FlowerSuggestion = "Daffodils";

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

                 FlowerSuggestion = "No suggestions";  

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