Conditional Processing in ColdFusion
What Is Conditional Processing?
Conditional processing is code that reacts to specified conditions and behaves differently based on those conditions. When application behavior depends on data that is not known at the time of development, such as user data or query information, conditional processing is an essential tool.
Performing Conditional Processing
ColdFusion employs a number of tags to implement conditional processing. The simplest is the <CFIF>/<CFELSE> tag set. In addition, the <CFSWITCH> tag is a better choice in some situations.
<CFIF>
The <CFIF> tag can take various forms. The simplest of them is as follows:
<CFIF expression> Code performed if condition true </CFIF>
The expression must evaluate to true or false. Traditionally, this is some condition using an operatorfor example, TheVar IS 40. Regardless of the outcome of the evaluation of the expression (either true or false), program flow continues after the </CFIF> tag.
Tip
Remember that ColdFusion does not use the normal symbols for operators. So the condition cannot use the = sign but uses the word IS. The operators are shown in the following table:
ColdFusion Operator |
Symbol |
IS, EQUAL, EQ |
= |
IS NOT, NOT EQUAL, NEQ |
<> |
GT, GREATER THAN |
> |
LT, LESS THAN |
< |
GTE, GREATER THAN OR EQUAL |
>= |
LTE, LESS THAN OR EQUAL |
<= |
Expressions to be evaluated as true or false can take many different forms, not just a condition using an operator. Many ColdFusion functions return a value that can be evaluated to TRUE or FALSE. Remember that numbers are inherently TRUE or FALSE in ColdFusion.
Tip
When ColdFusion is evaluating an expression to be TRUE or FALSE, be sure to remember the following:
Logically equivalent to TRUE:
Any nonzero numbers (including negative numbers)
YES
Logically equivalent to FALSE:
0 (Zero)
NO
The next step in using <CFIF> is to add an else clause. The logic then becomes an either/or situation. The general format is shown here:
<CFIF expression> Code performed if expression is true <CFELSE> Code performed if expression is false </CFIF>
With a single else clause, still only one expression is evaluated. The <CFIF> statement can be extended in yet another way, using <CFELSEIF>. This method offers the opportunity to have multiple conditions. The general format is as follows:
<CFIF expression1> Code performed if expression1 is true <CFELSEIF expression2> Code performed if expression2 is true<CFELSE>Code performed if no expressions are true </CFIF>
Note that the <CFELSE> is optional.
Tip
If you put the most common cases in the first <CFELSEIF>s, the amount of processing is minimized and performance is maximized.
<CFSWITCH>
You might be able to implement <CFIF> with multiple <CFELSEIF> clauses in a more readable and more efficient format by using <CFSWITCH>. Many languages refer to this logic as a CASE statement. The general format is as follows:
<CFSWITCH EXPRESSION="expression"> <CFCASE VALUE="value" DELIMITERS="delimiters"> HTML and CFML tags </CFCASE> additional <CFCASE></CFCASE> tags <CFDEFAULTCASE> HTML and CFML tags </CFDEFAULTCASE> </CFSWITCH>
With this control structure, you specify the value in each <CFCASE> statement that will possibly match the expression in the <CFSWITCH> line. The following is a very simple example:
<CFSWITCH EXPRESSION="World"> <CFCASE VALUE="Hello"> Hello was matched </CFCASE> <CFCASE VALUE="World"> World was matched </CFCASE> <CFDEFAULTCASE> Neither Hello nor World was matched </CFDEFAULTCASE> </CFSWITCH>
In this case, the code would display "World was matched". If neither Hello nor World were the expression in the <CFSWITCH>, the <CFDEFAULTCASE> would apply. Just as <CFELSE> is optional in <CFIF>, so <CFDEFAULTCASE> is optional when you're using <CFSWITCH>.
Note
In the <CFCASE> statement, equality is the only valid operator. You cannot use GT or LTE, for example, as the operator.
Multiple values can be listed for one case.
A DELIMITERS attribute with <CFCASE> specifies the character that separates multiple entries in the list of values. The default delimiter is the comma.
Tip
As with <CFIF>, it is best to put the most common cases at the top of the <CFSWITCH> to increase performance.
Nested Statements
Both the <CFIF> and <CFSWITCH> tags can be nested. In <CFIF>, one of the true conditions can be code that uses another <CFIF>. Likewise, in <CFSWITCH>, the code executed when a case is true can be another <CFSWITCH>.
More Details of Boolean Expressions
Boolean expressions are expressions that will be evaluated to either true or false. We've already discussed some details concerning evaluation of Boolean expressions, but other details are essential for understanding.
Logical Operators
ColdFusion has a set of logical, or Boolean, operators that are used with Boolean operands. They perform logical connective and negation operations. The most common of them are AND, OR, and NOT.
The operator AND returns TRUE if both operands are true; otherwise, FALSE is returned.
The operator OR returns TRUE if either operand is true. FALSE is returned only if both operands are false.
The NOT operator negates the operand. For instance, NOT FALSE returns TRUE.
Other logical operators are XOR, EQV, and IMP.
Parentheses
The standard rules for operator precedence (which operator is performed before others) are enforced in ColdFusion. If you want to change the order, you must use parentheses. For instance, AND is evaluated before OR. If you want to change this order, you could use parentheses as follows:
(A OR B) AND C
If parentheses are nested, the innermost set is evaluated first.
Short-Circuit Evaluation
When you use the logical operator AND, if the first operand is false, ColdFusion doesn't really need to do any further evaluation because FALSE joined with any operand using the AND operator is going to be false. ColdFusion realizes this and uses short-circuit evaluation, which means that the logical expression is evaluated only as far as necessary to determine the truth value of the whole expression.