Home > Articles > Mobile Application Development & Programming > HTML5 & Web Standards

Using CFScript

  • Print
  • + Share This
From the author of

From the author of

Conditional Processing

You can use conditional processing inside script blocks. You can make both if/else and switch/case statements. The syntax is a bit different, so let's see how a simple if statement looks first (see Listing 2).

Listing 2: An if Statement in CFScript

<cfset "MyNumber" = 9>

<cfscript>
   // check if the number is less than 10

   if (MyNumber lt 10) {

//if it is, output a message to the browser

   WriteOutput("Your number is less than 10");
   }
</cfscript>

To use conditional processing, you type if, followed by the condition that you wish to test for. The you use curly brackets to surround the code you want to run if the condition is true. In this case, we use the WriteOutput function to output a message to the browser. This function is similar to Write in JavaScript. Then we end the statement with a semicolon, as always, end the brackets, and close the </cfscript> block.

NOTE

Curly brackets are not required, but they help keep your statements organized. It's generally considered good practice to use them.

Because you cannot use tags such as <CFOUTPUT> inside ColdFusion script blocks, you use the WriteOutput function to accomplish this task. It works much like <CFOUTPUT> does, so if you load the above script in your browser, you'll see the message:

Your number is less than 10

Note that when using WriteOutput, you must enclose what you want to output in parentheses and double quotes. If the string inside your WriteOutput function contains quotes, you must escape them, like this:

<cfset "MyNumber" = 9>

<cfscript>
   // test for the variable's existence
   if (MyNumber lt 10) {
   
WriteOutput("She said, ""Your number is less than 10"".");
   }
</cfscript>

You can create additional conditional clauses using else and elseif, as Listing 3 shows.

Listing 3: Using if-elseif-else Inside CFScript

<cfset "MyNumber" = 256>

<cfscript>
   // test for the variable's existence
   if (MyNumber lt 10) {
   WriteOutput("She said, ""Your number is less than 10"".");
   }
   else if (MyNumber lt 20) {
   WriteOutput("Your number is between 10 and 19");
   }
   /* note that we can use HTML tags and variables
   set outside the script block in our WriteOutput */

   else {
   
   WriteOutput("Your number, <b>#MyNumber#</b>, is greater than 
        20");
   }
</cfscript>

Here's the output to the browser:

Your number, 256, is greater than 20

As you notice, the rules regarding use of expressions and operators is the same as in ColdFusion Markup.

switch/case in CFScript

There is a significant difference between how you use the switch case tags in CFML and how you perform the same operation in CFScript. You must use a BREAK statement inside each CASE. The BREAK statement tells ColdFusion to exit the switch case once a condition has been satisfied. If you don't include the BREAK statement, ColdFusion will not throw an error: It will run the DEFAULT case in addition to the CASE that evaluated true. Here is the syntax, which is exactly the syntax of a switch/case in Java:

Listing 4: switch/case

<cfscript>
   // set a variable to test against
   MyNumber = 18;
   
   /* unlike in regular CFML, 
     the switch expression needn't be
     surrounded with pound signs */
   
   switch (MyNumber) {
   
     case 1: {
     // if the number is 14, this code will run
     
     /* note how the case value is referenced
        we don't use the "value" keyword, 
        but use colons */
     
     WriteOutput("Your number is ONE.");
     /* use break to exit the case. since it is a statement 
        too, end it with a semi-colon */
     break;
        }
   
     case 2: {
     
     // we can use multiple values for one case
     WriteOutput("Your number is TWO");
     break;
        }
        
     default: {
     // note that we don't use "defaultcase", but simply 
     // "default"
     WriteOutput("Your number is neither one nor two.");
     }
   }
</cfscript>

And here is the resulting output to the browser:

Your number is neither one nor two.

Note that we are testing for numeric values here. If your CASE statement was testing against a string, you would need to use quotation marks around the value.

In general, it is preferable to use switch/case in its tag form because it processes faster and you are not as limited by the script syntax, which does not allow you to specify multiple values for a case.

  • + Share This
  • 🔖 Save To Your Account