- What Is Conditional Processing?
- Performing Conditional Processing
- Summary
Performing Conditional Processing
ColdFusion employs a number of tags to implement conditional processing. The simplest is the <cfif>/<cfelse> tag set. In addition, in some situations the <cfswitch> tag is a better choice.
<cfif>
The <cfif> tag can take various forms. The simplest is as follows:
<cfif expression> Code performed if condition true </cfif>
The expression must evaluate to TRUE or FALSE. Usually, this is some condition using an operator—for 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.
Table 3.1 lists the supported <cfif> operators:
Table 3.1. <cfif> Operators
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.
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 always optional.
<cfswitch>
If you have a long list of condition values to be tested, you might be better off using <cfswitch>. Many languages refer to this logic as a CASE statement. The general format is as follows:
<cfswitch expression="expression"> <cfcase value="value"> 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>.
The value passed to expression is just that, an expression. In the previous example, the expression was a string; to use a variable, it must be enclosed within pound signs (#).
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 the evaluation of Boolean expressions, but other details are essential to understand.
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 AND operator returns TRUE if both operands are true; otherwise, FALSE is returned.
The OR operator 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 (the level of priority in which operators are performed) are enforced in ColdFusion. If you want to change the priority, you must use parentheses. For instance, AND is evaluated before OR. If you want to change this in an expression, 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, ColdFusion doesn't really need to do any further evaluation if the first operand is false because when FALSE is joined with any operand using the AND operator, the expression 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 true value of the whole expression.