Conditional Programming

Perhaps the most important aspect of programming languages is the capability to program conditional logic. The most obvious of these is the If-Then-Else statement. Take a standard ColdFusion conditional statement like the following:

 <cfif x lt y>      <cfset index = x>  <cfelse>      <cfset index = y>  </cfif> 

In CFScript, it looks very much the same:

 <cfscript>      if (x lt y)         index = x;      else         index = y;  </cfscript> 

Let's look at a more complex example:

 <cfif action is "display_discount">       <cfset display_price = price   (price * discount)>           <cfelseif action is "display_sale">               <cfset display_price = price   (price * sale)>           <cfelseE>               <cfset display_price = price>  </cfif> 

In a script block, it would look like this:

 <cfscript>      if (action is "display_discount")          display_price = price   (price * discount);            else if (action is "display_sale")          display_price = price   (price * sale);      else          display_price = price;  </cfscript> 

For developers with formal programming backgrounds, the preceding code is a much cleaner, more succinct series of statements.

In the case of a compound statement, multiple lines are encapsulated within braces:

 <cfscript>      if (expression)          Statement;      else {          Statement1;          Statement2;      }  </cfscript>  

Inside ColdFusion MX
Inside Coldfusion MX
ISBN: 0735713049
EAN: 2147483647
Year: 2005
Pages: 579 © 2008-2017.
If you may any questions please contact us: