House of Fusion
Home of the ColdFusion Community
Hostmysite VPS Hosting
Subscribe Now
Fusion Authority Quarterly Update - ColdFusion 8 Special Edition

For ColdFusion hosting try HostMySite.com.
Search over 2,500 ColdFusion resources here  >>>      

CFSCRIPT: IF/ELSE Statement

6IF/ELSE Statement (Decision)
6.1 An IF statement in CFSCRIPT has the following format: An IF followed by an open and closed parenthesis. Between the open and closed parenthesis will go the entire comparison statement.
<CFSCRIPT>
     IF (Statement)
          operation;
</CFSCRIPT>
6.2As with other comparison locations (such as CFIF, While loops, etc.), a statement may be made up of many clauses.
<CFSCRIPT>
     IF (clause1 and clause2)
          operation;
     IF (clause1 or clause2)
          operation;
</CFSCRIPT>
6.3 IF code constructs follow the same rules as other comparison operations in ColdFusion. The use of short circuited boolean evaluation and functions in text comparisons should always be used.
6.3.1 Remember Short Circuited Boolean Evaluation - This feature was introduced in ColdFusion 4.01 and was a fantastic addition. It basically says that when a comparison statement (CFIF, CFELSEIF, IIF(), etc.) has more than one clause, the result of the first clause can effect if the second is run at all.
  • If the joiner is AND and the first clause is false, the entire statement is false and the second clause will never run.
  • If the joiner is OR and the first clause is true, then the entire statement is true and the second will never run.
  • If the joiner is IMP and the first clause is false, the entire statement is true and the second will never run.
In all cases, placing your clauses to take advantage of these rules can speed them up.
6.3.2 When doing comparisons between a text value and some other piece of data, use functions such as Len(), CompareNoCase(), and FindNoCase(). This is faster and more efficient.
6.4 The IF code construct has an ELSE construct that works with it. If the IF statement is false, the ELSE condition will occur.
<CFSCRIPT>
     IF (clause1)
          operation;
     ELSE
          operation2;
</CFSCRIPT>
6.5 There is currently no ELSEIF code construct in CFSCRIPT. This can be created by nesting IF and ELSE code constructs.
<CFSCRIPT>
     IF (clause1)
          operation;
     ELSE
          IF (clause1)
               operation2;
          ELSE
               operation3;
</CFSCRIPT>
6.6 Proper nesting is a must when dealing with multi-layered IF and ELSE code statements.
6.7 Multiple operations can be contained within an IF code statement by placing them within curly braces ({}).
<CFSCRIPT>
     IF (clause1)
    {
          operation1;
          operation2;
    }
</CFSCRIPT>
6.8 ALWAYS use IF code constructs over the IIF() function. The code statement (and tag for that matter) is more efficient.
6.9 When doing multiple comparisons against the same variable, the switch/case code statement may be more efficient and should be used.


User Contributed Notes


Add a Note


CFScript