Home > Articles

Implementing Conditional Processing

📄 Contents

  1. Hour 6: Implementing Conditional Processing
  2. Looping
  3. Summary
  4. Q&A
  5. Workshop
  • Print
  • + Share This
Conditional processing and looping are two basic and fundamental programming concepts. This sample chapter explains how to apply these concepts to dynamically generate your web page content.
This sample chapter is from Sams Teach Yourself ColdFusion Express in 24 Hours, by Ben Forta.
From the author of

Hour 6: Implementing Conditional Processing

Two basic and fundamental programming concepts are conditional processing and looping. These language features can help you dynamically generate your Web page content.

In this lesson, you'll learn the following:

  • How to code simple conditional statements

  • Programmatically selecting one of a set of conditions

  • How to make ColdFusion Express perform an action repeatedly in a loop

  • How to build pages that use conditional processing to alter the output returned to the browser

Decision Making

Having your programs make decisions, called conditional processing, is one of the basic building blocks of any programming language. With conditional processing, the program might take different paths and return different output based on some evaluated condition.

You perform conditional processing every day. You make many decisions based on conditional factors in your life. For instance, you might say "If it's raining out, I'll drive. Otherwise, I'll walk to work." If your life were a program, you could depict this decision as in Figure 6.1.

Figure 6.1 You can decide how to get to work based on the weather.

It's not important to really understand the actual symbols and structure of the flow diagram itself, but you can easily see that you are making a decision based on the condition of the weather forecast. This decision can lead you to either drive or walk to work.

Using the <CFIF> Tag

In programming, you can make a program take different paths or yield different output based on a condition. In CFML, the <CFIF> tag is used for this purpose. The <CFIF> tag is a container tag that has both a start and end tag:

<CFIF Expression IS "TRUE">
 Do something if the expression is true
</CFIF>

The <CFIF> tag works by evaluating an expression and concluding either a TRUE or a FALSE condition. Therefore, all expressions in the <CFIF> tag must return a TRUE or FALSE result. In this example, the variable WeatherForecast is either equal to "GoingToRain" or not, thus returning a TRUE or FALSE value:

<CFIF WeatherForecast IS "GoingToRain">
 Drive to work!
</CFIF>

Before we reach the decision to walk to work, I need to show you the types of conditions that can be evaluated. Each of these decisions must yield a TRUE or FALSE value.

NOTE

A value that is either TRUE or FALSE is also known as a Boolean value.

Several comparison operators allow you to return a TRUE or FALSE value to the <CFIF> tag. Each comparison operator, and an example, is listed in Table 6.1. Assume that the variable X has a value of 10 and FirstName has the value of Sue.

Table 6.1 Comparison Operators

Operator

Operation

Example

Yields

IS

Equality

<CFIF X IS 5>

FALSE

IS NOT

Inequality

<CFIF X IS NOT 5>

TRUE

GREATER THAN

Greater Than THAN 5>

<CFIF X GREATER

TRUE

LESS THAN

Less Than

<CFIF X LESS THAN 5>

FALSE

GREATER THAN OR or equal to

Greater than THAN OR EQUAL TO 5>

<CFIF X GREATER

TRUEEQUAL TO

LESS THAN OR or equal to

Less than OR EQUAL TO 5>

<CFIF X LESS THAN

FALSEEQUAL TO

CONTAINS comparison

String CONTAINS "Su">

<CFIF FirstName

TRUE

DOES NOT CONTAIN comparison

String DOES NOT CONTAIN

<CFIF FirstName

TRUE"Bu">


You could alter the conditional statement to drive to work only if it will rain more than two hours in a day:

<CFIF RainDurationHours GREATER THAN "2">
 Drive to work!
</CFIF>

As you can see by the table, these operators can get lengthy. If you are typing challenged, you might want to use the shorthand notation for some operators, as shown in Table 6.2.

Table 6.2 Shorthand Notation

Operator

Alternative Operator

IS

EQUAL, EQ

IS NOT

NOT EQUAL, NEQ

GREATER THAN

GT

LESS THAN

LT

GREATER THAN OR EQUAL TO

GTE, GE

LESS THAN OR EQUAL TO

LTE, LE


NOTE

The CONTAINS operator doesn't have a shorthand notation.

Another way to formulate the condition for driving if it rains less than two hours would be

<CFIF RainDurationHours GT "2">
 Drive to work!
</CFIF>

That'll save typing, but it is still understandable.

I'm sure you're wondering "Why can't I just use the operators I'm familiar with, such as the greater than symbol '>'?" There's a simple answer to this one—tag syntax.

If CFML allowed you to use the usual conditional operators, ColdFusion Express would be pretty confused trying to figure out where the tag ends:

<CFIF RainDurationHours > "2">
 Drive to work!
</CFIF>

In the previous example, ColdFusion Express would see the greater than symbol (>) and think the tag was being closed. Then it would get really confused trying to figure out what to do with the rest of the line, and a syntax error would occur.

For this reason, CFML doesn't allow the usual operators (=, <, >, <=, >=) in conditional expressions. This is one of the most common CFML programming mistakes, so keep your eye out for it.

The Fallback Decision

In CFML, there's a way to make a decision if all other conditions fail. This fallback decision is created using the <CFELSE> tag

Returning to the example of when to walk or drive to work, you are only deciding when to drive to work:

<CFIF WeatherForecast IS "GoingToRain">
 Drive to work!
</CFIF>

What happens if it is not going to rain? In this example, nothing. If we wanted the condition to perform another action when the <CFIF> statement is FALSE, we could use the <CFELSE> tag.

<CFIF WeatherForecast IS "GoingToRain">
 Drive to work!
<CFELSE>
 Walk to work!
</CFIF>

The <CFELSE> tag allows you to say what to do if the <CFIF> condition is FALSE. In this case, if WeatherForecast isn't GoingToRain, the program will suggest that you walk to work.

To Do: Creating <CFIF> Statements

Try writing a conditional statement by performing the following steps:

  1. Launch HomeSite.

  2. Create a new page using the default template.

  3. Change the <TITLE> value to "Deciding the Commute".

  4. Save the page in the /learncfe/hour4 directory under the Web root as cfif.cfm.

  5. Between the opening and closing <BODY> tags, type <CFSET WeatherForecast="MostlySunny">.

  6. Write a conditional statement that will check the WeatherForecast variable and print a message to walk if it will be mostly sunny, and drive otherwise. The code should look like the following:

    <CFSET WeatherForecast="MostlySunny">
    <CFIF WeatherForecast IS "MostlySunny">
     Walk to work!
    <CFELSE>
     Drive to work!
    </CFIF>
  7. Save the page and then browse the file to see what happens.

  8. Now go back to HomeSite and change the <CFSET> tag so that it sets the weather to "Raining".

  9. Save the page and refresh your browser to see the change in output.

Making a Decision from Multiple Choices

So far, we have only checked if WeatherForecast is "MostlySunny" and will drive as long as it isn't. What if you wanted to create multiple conditions and decide amongst them? You can take many different paths based on separate conditions in your life, and so can a ColdFusion Express program.

In order to create a conditional statement that would decide amongst more than one decision, you will need to use the <CFELSEIF> tag. This tag allows you to make any number of decisions to determine which one is true:

<CFIF WeatherForecast IS "GoingToRain">
 Drive to work!
<CFELSEIF WeatherForecast IS "MostlySunny">
 Walk to work!
<CFELSE>
 I'm not sure what to do, since I don't know what the weather is!
</CFIF>

TIP

Most developers indent their code, as seen in the previous example. This will greatly enhance the readability and maintainability of your code. You will also need to use indenting to separate <CFIF> statements that are nested inside one another, so get into the habit of using this convention.

In this example, you are making strict decisions based on the value of WeatherForecast. If both the <CFIF> and all the related <CFELSEIF> tags are FALSE, it will fall through and get processed by the <CFELSE> tag.

It is important to know that ColdFusion Express will only evaluate the <CFIF> statement until it finds the first true condition. Once found, the instructions in that condition will be executed and then ColdFusion Express will jump out of the <CFIF> statement.

Figure 6.2 depicts where processing is resumed after a true condition is evaluated.

Figure 6.2 After evaluation, ColdFusion Express resumes processing following the </CFIF> tag.

Nesting Decisions?

You not only make decisions every day, but also you make decisions based on other decisions that were already made. In the programming world, this is known as nesting decisions, or nested conditional processing.

Think, for example, that you might decide to walk to work in the rain if you have your galoshes at home. You might make the decision as depicted in Figure 6.3.

Figure 6.3 You might decide to walk to work in the rain if you have your galoshes.

The decision of whether it is going to rain determines if you make the next decision of walking to work if you have your galoshes. So the rules are as follows:

If it's going to rain
 If you have your galoshes
  You'll walk to work
 If you don't have your galoshes
  You'll drive to work
If it's not going to rain
  You'll walk to work.

You can start writing this code by making the outermost decision first—the one that you need to make before any others can be made:

<CFIF WeatherForecast IS "GoingToRain">
 ...
<CFELSEIF WeatherForecast IS "MostlySunny">
 Walk to work!
<CFELSE>
 I'm not sure what to do, since I don't know what the weather is!
</CFIF>

Now you can fill in the decision of whether you have your galoshes:

<CFIF WeatherForecast IS "GoingToRain">
 <CFIF Galoshes IS "Yes">
  Walk to work!
 <CFELSE>
  Drive to work!
 </CFIF>
<CFELSEIF WeatherForecast IS "MostlySunny">
 Walk to work!
<CFELSE>
 I'm not sure what to do, since I don't know what the weather is!
</CFIF>

Once again, notice the indention of the code. This makes it very easy to read and decipher later on.

You can nest as many <CFIF> statements as you need to make the decision.

CAUTION

ColdFusion Express will support as many levels of nested conditional statements as you'd like. But be careful when doing so. It is very easy to introduce bugs when writing complex nested statements. In addition, the more you nest the harder you make ColdFusion Express work, and that could harm the performance of your application.

Compound Conditions

You can also make a decision that is a result of a few conditions being true within the same <CFIF> tag. This compound condition can be implemented using a logical operator. Logical operators allow you to combine two or more comparisons into a single decision.

The logical operators are AND, OR, and NOT. With these three operators, you can make a decision based on multiple conditions. Logical operators and examples are shown in Table 6.3. Assume that X=1 and Y=5.

Table 6.3 Logical Operators

Operator

Description

Use

Yields

AND both sides are

Produces TRUE if TRUE.

<CFIF X IS 1 AND Y IS 2>

FALSE

OR either side is

Produces TRUE if TRUE.

<CFIF X IS 1 OR Y IS 2>

TRUE

NOT opposite TRUE or

Produces the FALSE results.

<CFIF NOT(X IS 1)>

FALSE


If you will walk to work when it is sunny or you have your galoshes with you, you could write the <CFIF> tag as such:

<CFIF WeatherForecast IS "MostlySunny" OR Galoshes IS "Yes">
 Walk to work!
<CFELSEIF WeatherForecast IS "GoingToRain">
 Drive to work!
<CFELSE>
 I'm not sure what to do, since I don't know what the weather is!
</CFIF>

As in the previous code, most times nested <CFIF> logic can be replaced with the correct logical operator.

CAUTION

In this case, the order of the <CFIF> and <CFELSEIF> conditions is very important. If you were to reverse the conditions as in the following code, in rainy weather you would drive to work regardless of whether you had your galoshes:

<CFIF WeatherForecast IS "GoingToRain" >
 Drive to work!
<CFELSEIF WeatherForecast IS "MostlySunny" OR Galoshes IS "Yes">
 Walk to work!
<CFELSE>
 I'm not sure what to do, since I don't know what the weather is!
</CFIF>

TRUE and FALSE Values

Up to this point, I've told you and shown you that the <CFIF> tag only can accept values of TRUE and FALSE. That is correct, but what I haven't told you is that there are other values that are equivalent to TRUE and FALSE. Examine the following code:

<CFSET X="5">
<CFIF X>
 X is true!
<CFELSE>
 X is false!
</CFIF>

Because the variable X is set to "5", you might think that the output will be "X is false!". In fact, the result is that X is TRUE. What could account for this?

In CFML, as well as other programming languages, the value TRUE is equivalent to a non-zero value—as long as the value is non-zero it will evaluate to TRUE. The number can even be a negative number and it will evaluate to TRUE. Table 6.4 shows the equivalent values for TRUE and FALSE.

Table 6.4 TRUE/FALSE Equivalent Values

TRUE/FALSE

Equivalent

TRUE

Any non-zero number, YES

FALSE

Zero, NO


As seen in this table, the string YES is also equivalent to TRUE, and NO is equivalent to FALSE.

We will take a look at an example of a function called IsNumeric(). You will look at functions in depth in Hour 7, "Using Functions," but suffice it to say that this function will take a variable value and return an indicator if the value is a number. In this example, we are testing the value of a variable X and storing the result of the test into the Result variable:

<CFSET X="5">
<CFSET Result=IsNumeric(X)>
<CFOUTPUT>Result = #Result#</CFOUTPUT>

NOTE

Notice that the IsNumeric(X) function doesn't require double quotes around it in the <CFSET> tag. This is because it is not a literal value but instead needs to be evaluated by ColdFusion Express.

The output is depicted in Figure 6.4.

Figure 6.4 The IsNumeric() function returns a YES or NO.

As you can see by the output, the IsNumeric() function returns a YES if the variable is a number (and NO otherwise). Because this function returns a value that <CFIF> can directly evaluate, you can use this function inside the tag:

<CFSET X="5">
<CFIF IsNumeric(X)>
 X is a number!
<CFELSE>
 X is not a number!
</CFIF>

TIP

You could put the full evaluation into the <CFIF> tag:

<CFSET X="5">
<CFIF IsNumeric(X) IS "TRUE">
 X is a number!
<CFELSE>
 X is not a number!
</CFIF>

Using this syntax ColdFusion Express will have to do extra work to get the result, which is YES, and convert it to the value TRUE. For this reason, just using the function performs better.

SWITCHing Gears

There is another <CFML> tag that you can use to make a decision ba

sed on the outcome of a comparison—the <CFSWITCH> tag. This tag allows you to make a decision about one condition and perform a task based on its value.

The <CFSWITCH> tag is a container with opening and closing tags:

<CFSWITCH EXPRESSION="expression">
 cases for each value of expression...
</CFSWITCH>

The biggest difference between the <CFIF> construct and the <CFSWITCH> tag is that <CFSWITCH> can only evaluate one expression at a time. The expression is set in the Expression attribute and all cases compare to that value.

If you wanted to evaluate the value of the WeatherForecast variable, you could code the tag in this way:

<CFSET WeatherForecast="MostlySunny">
<CFSWITCH EXPRESSION="#WeatherForecast#">
 cases for each value of expression...
</CFSWITCH>

CAUTION

Notice that the Expression attribute value has pound signs around the variable #WeatherForecast#. This is a quirk about the <CFSWITCH> tag. You must tell it to take the value of the variable instead of assuming that it will do so on its own. This is a very common error that affects both new and seasoned CFML programmers alike, so beware!

In order to decide what to do for each value of the expression (in this case, the value of the WeatherForecast variable), you would use the <CFCASE> tag. The format of this tag is

<CFCASE VALUE="ValueToCompare">
 What to do if Expression=ValueToCompare
</CFCASE>

If you wanted to decide what to do for each value of WeatherForecast, you could code the full <CFSWITCH> tag as follows:

<CFSET WeatherForecast="MostlySunny">
<CFSWITCH EXPRESSION="#WeatherForecast#">
  <CFCASE VALUE="MostlySunny">
   Walk to work!
  </CFCASE>
  <CFCASE VALUE="GoingToRain">
   Drive to work!
  </CFCASE>
</CFSWITCH>

If you wanted to walk to work if you had your galoshes, you couldn't use the <CFSWITCH> tag because it only allows one expression to be evaluated.

TIP

The <CFSWITCH> tag performs better than the <CFIF> tag. So use it whenever you have one expression that needs to be evaluated.

In order to determine what to do if all <CFCASE> values aren't matching the expression, you can use the <CFDEFAULTCASE> tag. This tag will be the fallback decision if all other decisions are FALSE:

<CFSET WeatherForecast="MostlySunny">
<CFSWITCH EXPRESSION="#WeatherForecast#">
<CFCASE VALUE="MostlySunny">
 Walk to work!
</CFCASE>
<CFCASE VALUE="GoingToRain">
 Drive to work!
</CFCASE>
<CFDEFAULTCASE>
  I'm not sure what to do, since I don't know what the weather is!
</CFDEFAULTCASE>
</CFSWITCH>
  • + Share This
  • 🔖 Save To Your Account