
Understanding conditional processing
27
You use conditional processing to customize the behavior of your application.
Conditional
processing
facilitates decision making and lets you control how the code on a page is processed.
In ColdFusion, you implement conditional processing with flow control tags. These tags are
similar to other programming language control elements, such as
if
,
then
, and
else
.
Conditional processing tags
ColdFusion provides several tags that let you control how a page is processed. When using these
tags, you can facilitate decision making in your code. The most fundamental tags used to control
code execution are the
cfif, cfelse,
and
cfelseif
tags. Because you will see and use these
tags in Part II of this book, the following sections provide a basic introduction on how to use
these tags. For more information about other conditional processing tags, including tags for
looping, see
Developing ColdFusion MX Applications
.
Using cfif to evaluate True or False conditions
To create statements that let you evaluate conditions and perform an action based on the result,
you use the
cfif
tag to create a
cfif
statement. The basic syntax is as follows:
<cfif
expression
>
HTML and CFML tags executed if expression is True.
</cfif>
In the previous example, ColdFusion only executes the code inside the
cfif
statement if the
expression evaluates to True. To perform actions if the expression is False, you can use the
cfelse
tag. For example, if the following
cfif
expression evaluates to False, then the code between the
cfels
e tag and the
cfif
tag is processed:
<cfif
expression
>
HTML and CFML tags executed if expression is True.
<cfelse>
HTML and CFML tags executed if expression is False.
</cfif>
Using cfelseif to evaluate multiple expressions
To evaluate multiple expressions in a
cfif
statement, you can use
cfelseif
and
cfelse
in your
statement; for example:
<cfif
expression 1
>
HTML and CFML tags executed if expression 1 is True.
<cfelseif
expression 2
>
HTML and CFML tags executed if expression 2 is True.
<cfelse>
HTML and CFML tags executed for expression(s) that is False.
</cfif>
The following example shows you how you can evaluate multiple expressions using these tags. In
this example, you created a form in which users can enter their state to determine their state tax:
<cfoutput>
<cfif form.state IS "MA">
#form.state# State Tax: 8.5%
<cfelseif form.state IS "VA">
#form.state# State Tax: 8.2%
<cfelse>
#form.state# State Tax Unknown
</cfif>
</cfoutput>
Summary of Contents for COLDFUSION MX 61-GETTING STARTED BUILDING COLDFUSION...
Page 1: ...Getting Started Building ColdFusion MX Applications...
Page 6: ...6 Contents...
Page 10: ......
Page 30: ...30 Chapter 2 CFML Basics...
Page 36: ...36 Chapter 3 Database Fundamentals...
Page 48: ......
Page 76: ...76 Chapter 6 Lesson 2 Writing Your First ColdFusion Application...
Page 134: ...134 Index...