Conditional Processing


Conditional processing with <cfscript> is similar to its function in both Java and JavaScript. Much like with CFML syntax, a conditional statement can be constructed in two ways: using either if or switch/case.

if Examples

A sample if statement appears as follows:

 <cfscript>  x = 1;  if(x is 1)  WriteOutput("In Spanish, the number one is 'uno.'<br>");  //above line above will run if x is 1  WriteOutput("Thanks for using our Spanish number converter.<br>");  // above line will always run </cfscript> 

The preceding statement evaluates x in the if statement and prints "In Spanish, the number one is 'uno.'" to the browser. Note that no semicolon follows the if statement. There is also no such thing as an endif statement. All conditional processing statements run the next line if the expression is true. Therefore, the line that sends the Spanish output to the browser runs only if x is 1. The second printed statement, however, always runs.

To add statements to the conditional so that more than one line relies on x equaling 1, you must use curly braces like this:

 <cfscript>  x = 1;  if(x is 1) { //note the curly braces start here  WriteOutput("In Spanish, the number one is 'uno'.<br>");  WriteOutput("Uno happens to mean 'one' in Italian as well.<br>");  //both of these lines will run if x is 1  } // the curly braces end  WriteOutput("<br>thanks for using our Spanish number converter.");  // this line will ALWAYS run whether x is 1 or not </cfscript> 

Predictably, else and elseif statements are similar to the preceding example, as you can see in the following code. Using curly braces is recommended to keep things in order, but it is not required:

 <cfscript>  x = 1;  if(x is 1)  {  WriteOutput("In Spanish, the number one is 'uno'.<br>");  WriteOutput("Uno happens to mean 'one' in Italian as well.<br>");  }  else if (x IS 2)  WriteOutput("In Spanish, the number two is 'dos'.<br>");  //no curly braces needed, but use to better organize code  else  // the line below runs only if x is neither 1 nor 2;  WriteOutput("I don't know what that number is in Spanish.<br>");  // the line below ALWAYS runs whether x is 1, 2, or something else  WriteOutput("<br>thanks for using our Spanish number converter."); </cfscript> 

NOTE

You should notice a few things about the script blocks used so far. First, all conditional statements use the same operators as CFML. It is illegal to use = or <, but legal to use the operators is or lt.

Second, <cfscript> cannot have a <cfoutput> block inside it and is therefore incapable of outputting to the browser. To accomplish this task, you can use the WriteOutput() function.

Finally, although <cfoutput> is not allowed within a script block, it is completely legal outside a block, as is any other CFML tag. By nesting a <cfscript> tag inside a <cfoutput> or <cfloop> tag, you can loop over the script statement.


Conditional operators were covered in Chapter 3, "Conditional Processing." <cfloop> was covered in Chapter 4, "Looping," and <cfoutput> looping techniques were discussed in Chapter 7, "Using Databases."


switch/case Examples

A switch/case in ColdFusion script is accomplished a bit differently than in CFML. Again, because you cannot use any tags in a <cfscript> block, you can turn to the following example in lieu of <cfswitch>:

 <cfscript>  x = 1;  switch(x) {  case 1: {  WriteOutput("In Spanish, the number one is 'uno'.");  WriteOutput("Uno happens to mean 'one' in Italian as well.");  break;  }  case 2: {  WriteOutput("In Spanish the number two is 'dos'.");  break;  }  default: { WriteOutput("I don't know that number.");} } </cfscript> 

TIP

An important difference between the preceding example and the normal use of a <cfswitch> tag involves the break statements needed within each case. If you do not place the break statements in each case block, the default always runs in addition to the true case expression. You can use a missing break tag to your advantage if more than one case tag is to be observed. For instance, within the case 1 statement, you could set x = 2. If case 1 doesn't have any break statements, the new value of x would lead to both case 1 and case 2 running.

Because ColdFusion runs from top to bottom, having a case 2 with x set back to 1 would not cause case 1 to run over again.




Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net