Conditional Processing Using CFSWITCH

Share Button

If you’ve read through my ColdFusion Programming 101: Basic Concepts: Conditional Processing, you should already be pretty familiar with conditional processing and using the CFIF /CFELSE/CFELSEIF tag combinations to process your ColdFusion code in the order you want it to be processed. If you haven’t read it, I highly recommend it to give you a good understanding of conditional processing. But if you need just a quick refresher on what conditional processing is it simply means that the code is read by the application, will react to any conditions it comes across and will behave the appropriate way.

We can process conditional statements by using the now familiar CFIF statement sets, but there is still another way by using the CFSWITCH tag.

<!--- Set the catalog type to show--->
<cfset catalogtype = "Fashion">

<cfswitch expression="#catalogtype#">

    <cfcase value="Furniture">
    Displaying the Furniture Catalog.
    </cfcase>
    
    <cfcase value="Fashion">
    Displaying the Fashion Catalog.
    </cfcase>
    
    <cfcase value="Children">
    Displaying the Children's Catalog.
    </cfcase>
    
    <cfdefaultcase>
    Displaying all catalogs.
    </cfdefaultcase>
    
</cfswitch>

Taking a look at this code, we start off with setting a variable called catalogtype equal to the value “Fashion”. This will more than likely be dynamic in a finished application but for the purpose of this tutorial, we will just manually set the value so we have something to work.

Next, we start the processing by adding a <cfswitch> tag and the expression which is the value of the variable we just assigned…currently “Fashion”. All we are telling ColdFusion to do at this point is get ready to process some code, based on the value contained in the “expression”.

For each condition we want to check for we are going to use a <cfcase> tag along with the value we want to check. For example, in the first <cfcase> we are simply asking ColdFusion “Is the value ‘Furniture’?” If the answer is yes (or true) it would process that block of code.

<cfcase value="Furniture">
    Displaying the Furniture Catalog.
</cfcase>

We of course know that this block of code would never process in its current state because the value of the expression we are checking is “Fashion” and not “Furniture”. In this case, the <cfwitch> process would skip that particular <cfcase> and move onto the second <cfcase>. The second <cfcase> is checking for the value of “Fashion” and since our expression does return true, it would execute all code within that <cfcase> tag and then exit the entire <cfswitch> processing.

What happens when none of the cases match the expression being sent to it? It could just skip right over the entire block of code and not process anything. However, we have an optional tag <cfdefaultcase> that will execute that block of code if no other cases within the <cfswitch> tag return true.

<cfdefaultcase>
    Displaying all catalogs.
</cfdefaultcase>

The end result of using the <cfswitch> tag is exactly the same as using the <cfif> tag, so why use <cfswitch> over <cfif>? First, <cfswitch> is actually faster to process than using the <cfif> tag. Second, the ColdFusion code for writing <cfswitch> tends to be neater and easier to read by a programmer.

Having said that, the only problem with using <cfswitch> is that you can’t use it in all cases of conditional processing. When using <cfif> you can check for different conditions with each instance of the <cfelseif> tag. For example, I can check if the month is January and if that comes back false it will move onto the next condition to check if the year is 2013. With <cfswitch>, you can only check for one expression and have multiple <cfcase> tags per expression.

Now that you know how to use both CFIF and CFSWITCH to process your application, in the next lesson, we will go over conditional processing using cfscript coding methods.


Leave a Reply

Your email address will not be published. Required fields are marked *