• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Step 4. Controlling Program Flow > Using Conditional Statements

Using Conditional Statements

There are several techniques we can use to control the flow of our application, and they all revolve around the concept of conditional statements. In its simplest form, a conditional statement compares two values and performs some action based on the outcome. The following text illustrates an example of conditional statements written in plain English:

If a user is not logged in, redirect him to the login page.

If he is logged in, proceed to the menu page.

If we were to apply this to ColdFusion, we would be checking the value of some variable (perhaps UserIsLoggedIn) and then taking one of two different actions, depending on whether the value was True or False.

Comparison Operators

All conditional statements rely on comparing at least two values, and there are several standard comparisons we can use to do this. Table 4.1 lists the comparison operators we can use with ColdFusion.

Table 4.1. Comparison Operators
Operator Name Shorthand Description
EQUAL EQ, IS Tests to see whether the two values are equal.
NOT EQUAL IS NOT, NEQ Tests to see whether two values are not equal.
GREATER THAN GT Tests to see whether the value on the left of the operator is greater than the value on the right.
GREATER THAN OR EQUAL GTE Tests to see whether the value on the left of the operator is greater than or equal to the value on the right.
LESS THAN LT Tests to see whether the value on the left of the operator is less than the value on the right.
LESS THAN OR EQUAL LTE Tests to see whether the value on the left of the operator is less than or equal to the value on the right.
CONTAINS  Tests to see whether a value is contained in another value. For example, does an email address contain the @ symbol?
DOES NOT CONTAIN  Tests to see whether a value is not contained within another. This is the opposite of CONTAINS.


To help speed up coding, most operators have a shorthand abbreviation. For example, the following two blocks of code mean the same thing and evaluate to the same value.

<CFIF Quantity GREATER THAN OR EQUAL 10> 
      You get a discount 
</CFIF> 


<CFIF Quantity GTE 10> 
      You get a discount 
</CFIF>

Using the shorthand symbols simply saves typing time.

Logical Operators

There are also a number of Boolean or logical operators that we can use when comparing values. Table 4.2 lists the more commonly used logical operators.

Table 4.2. Logical Operators
Operator Description
AND Tests to see whether the conditions on both sides of the AND operator are true. For example, “This is true and so is that.”
OR Tests to see whether the conditions on either side of the OR operator are true. For example, “This could be true or that could be true.”
NOT Negates other logical operators.


The following code illustrates how we can use logical operators to string several conditions together.

<CFIF (FollowingRules IS "No") OR (IDontLikeYourFace IS "Yes")> 
      No soup for you! 
</CFIF>

In this example, we only have to blow it on one count: Either we are not following the rules, or somebody doesn’t like our face. With either one, we miss out on soup.

NOTE

It is important to point out that all of these comparisons evaluate to either TRUE or FALSE. For example, the comparison <CFIF Quantity GTE 10> returns the value of TRUE if we order a quantity of 10 or more and returns FALSE if we order less than 10.

Remember, too, that ColdFusion treats FALSE, NO, and the numerical value of 0 all as the same value. ColdFusion also treats TRUE, YES, and any nonzero number the same.


  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint