Home > Articles

Implementing Conditional Processing

📄 Contents

  1. Hour 6: Implementing Conditional Processing
  2. Looping
  3. Summary
  4. Q&A
  5. Workshop
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.
Like this article? We recommend

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>

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020