• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Step 4. Controlling Program Flow > Using SWITCH/CASE Statements

Using SWITCH/CASE Statements

You can see that IF statements can become quite complex when you have to check for a lot of conditions. If you have a standard set of responses for which you want to check, it will probably be easier and quicker to use a SWITCH/CASE comparison. For example, it wasn’t just my father I had to contend with. Whenever I came home with my report card, my Mother would be waiting there ready to get in on the act. If she had been a ColdFusion programmer, her code would have looked something like this:

<!--- A SWITCH / CASE block to check my English grade ---> 
<CFSWITCH EXPRESSION=#EnglishGrade#> 
<CFCASE VALUE="A"> 
            <!--- run this code if I got an A (yeah right) ---> 
            You are a very bright boy, you get that from my 
            side of the family. 
      </CFCASE> 

      <CFCASE VALUE="B"> 
            <!--- run this code if I got a B ---> 
            What happened to the A? 
      </CFCASE> 

      <CFCASE VALUE="C"> 
            <!--- run this code if I got a C ---> 
            You better bring these grades up or you will 
            wind up being a Web developer! 
      </CFCASE> 

      <CFCASE VALUE="D"> 
            <!--- run this code if I got a D ---> 
            You are just like your Father. 
      </CFCASE> 

      <CFCASE VALUE="E"> 
            <!--- run this code if I got an E and could not doctor 
                  the report card to make it look like a B ---> 
            Where did we go wrong? 
      </CFCASE> 

      <CFCASE VALUE="F"> 
            <!--- big trouble ---> 
            Oh! I feel faint. Call an ambulance! 
      </CFCASE> 

</CFSWITCH>

					  

The preceding code checks a variable called EnglishGrade (and believe me it did vary) and checks a list of standard responses. When a match is found, the appropriate code is run.

Using <CFSWITCH> and <CFCASE>

The <CFSWITCH> and <CFCASE> tags work together and cannot be used on their own. <CFSWITCH> is a container tag and has only one attribute—EXPRESSION. The EXPRESSION attribute is required and sets the variable or condition you want to check against.

The <CFCASE> tag is also a container tag. The opening and closing <CFCASE> tags must be nested within the opening and closing <CFSWITCH> tags. The <CFCASE> tag has two attributes: VALUE, which is required, and DELIMITERS, which is optional.

The VALUE attribute of the <CFCASE> tag is the value we want to check against the EXPRESSION attribute of the <CFSWITCH> tag. In the preceding example, we were checking values, such as A and B against the <CFSWITCH> EXPRESSION attribute, #EnglishGrade#. When the VALUE attribute of the <CFCASE> tag and the EXPRESSION attribute of the <CFSWITCH> tag match, the code between that particular pair of <CFCASE> tags executes. When this code is finished executing, we jump to the end of the SWITCH/CASE block and begin to run the rest of the code in the template.

<CFDEFAULTCASE>

What if we don’t find a match with our <CFCASE> tags? For example, what if my #EnglishGrade# was a G or was not on my report card at all? If a match is not found with a <CFCASE> tag, the <CFSWITCH> block has no effect, and the code execution picks up directly after the closing </CFSWITCH> tag.

If we want to specify a default action if no match is found, we can use the <CFDEFAULTCASE> tag. The <CFDEFAULTCASE> tag is a container tag, just like the <CFCASE> tag. The <CFDEFAULTCASE> tag must be nested within the <CFSWITCH> tags and must be placed below all the other <CFCASE> tags. This tag has no attributes.

After a few years of me wearing her down, my Mother’s criteria toward my grades changed, and she just wanted to make sure I wasn’t going to be held back. This is shown in the following code:

<!--- A SWITCH / CASE block to check my English grade ---> 
<CFSWITCH EXPRESSION=#EnglishGrade#> 
      <CFCASE VALUE="A,B,C"> 
            <!--- run this code if I got an A, B or C ---> 
            Yeah, all right. 
      </CFCASE> 

      <CFCASE VALUE="D,E,F"> 
            <!--- run this code if I got a D, E or F---> 
            Off to summer school. 
      </CFCASE> 
      <CFDEFAULTCASE> 
            Hey where is your English grade? Have you been skipping school again? 
      </CFDEFAULTCASE> 

</CFSWITCH>

					  

In this code listing, you will notice a few new things. First, notice that you can use <CFCASE> to perform a comparison on several values at once. To do this, you just put all the values you want to compare against the <CFSWITH> expression into one <CFCASE> tag and separate the values with commas. Second, note that if the code doesn’t find an EnglishGrade of A, B, C, D, E, or F, it uses <CFDEFAULTCASE> to launch into an investigation of my whereabouts during the semester.

NOTE

If you want to use a list of values in a <CFCASE> tag, but you want to separate those values with something other than a comma, you can use the DELIMITERS attribute to specify a different delimiter. For example, the following code illustrates how to use a semicolon as a delimiter to separate values:

<CFCASE VALUE="A;B;C" DELIMITERS=";"> 
      Yeah, all right. 
</CFCASE>


  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint