break


break

The break statement is used when the loop must be ended before the condition for the loop is false. In this case, the insertion of a break statement will cause the loop to end, and any code after the break that is within the loop will not be executed. For example:

 <cfscript>      while(condition){          if(condition)         break;          else{           statement;          }      }  </cfscript>  

continue

The continue statement is used when the rest of the code in the loop does not need to be executed, but the loop must continue to run until the condition is false. In this case, the continue statement is used much like the break statement:

 <cfscript>      while(condition){          if(condition)              continue;          else{          statement;          }      }  </cfscript>  

switch

As we saw earlier, If-Then-Else statements can be easily coded using CFScript. However, complex If structures can be difficult to code and difficult to make sense of. In this case, it might be worthwhile to use a switch statement. The tag-based version of a switch statement is the CFSWITCH tag. This tag takes a variable, and, depending on the value, will perform certain actions, or cases. For example:

 <cfswitch expression="#somevar#">  <cfcase value="one">      Statement  </cfcase>  </cfcase value="two">      Statement  </cfcase>  <cfdefaultcase>      Statement  </cfdefaultcase>  </cfswitch> 

Here, we are switching on the value of the variable "somevar". If its value is "one", we will use the first case, and so on. If no other cases fit the value of "somevar", we will do what is contained in the CFDEFAULTCASE statements. If the default case is left out and no other case fits, no code will be executed within the switch. The CFSCRIPT version is very similar:

 <cfscript>      switch (somevar){          case "one": {        Statement;       break;          }          case "two": {       Statement;       break;          }          default: {       Statement;       break;           }      }  </cfscript> 

Note the use of the break statement at the end of each case. You should almost always put a break statement at the end of the case statement because it needs to tell ColdFusion to exit the switch statement. The break statement is optional, but if you do not use it, ColdFusion will execute all the statements in the following case statement, even if that case is false. This is something you more or less never want to do.



Inside ColdFusion MX
Inside Coldfusion MX
ISBN: 0735713049
EAN: 2147483647
Year: 2005
Pages: 579

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