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 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's operators are different to what you may be used to in other programming languages. For example, a condition cannot use the = sign, but instead uses the word IS. The operators are shown in the following table.


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.

TIP

When ColdFusion is evaluating an expression to be trUE or FALSE, be sure to remember the following:

Logically equivalent to trUE:

  • TRUE

  • Any nonzero numbers (including negative numbers)

  • YES

Logically equivalent to FALSE:

  • 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 always optional.

TIP

If you put the most common cases in the first <cfelseif>, the amount of processing is minimized and performance can be improved.


<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 (#).

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.


NOTE

Not every <cfif> statement can be replaced with a <cfswitch> statement (although every <cfswitch> can be replaced by <cfif>). The big difference between <cfif> and <cfswitch> is in what you can test for. <cfswitch> can only test for different values in the same expression, whilst <cfif> has no such restrictions.


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.



Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

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