Flow Control
- To work with <cfif><cfelseif><cfelse> blocks.
- To work with <cfswitch><cfcase> blocks.
- To create a self-processing form page.
- To use loops in ColdFusion.
- To set defaults for variables.
- To use <cflocation> to redirect pages.
Conditional Processing
Conditional processing allows programmers to output different code based on specific conditions. There are two conditional structures in ColdFusion <cfif> blocks and <cfswitch> blocks.
If-Else if-Else Blocks
Simple <cfif>
The syntax for a simple <cfif> condition is shown below:
<cfif conditions> Hello World! </cfif>
In the above code, Hello World will be displayed only if the conditions are true.
<cfif><cfelse></cfif>
The syntax for a simple <cfif><cfelse> condition is shown below:
<cfif conditions> Hello World! <cfelse> Goodbye World! </cfif>
<cfif><cfelseif><cfelse></cfif>
The syntax for a simple <cfif><cfelse><cfelseif> condition is shown below:
<cfif conditions> Hello World! <cfelseif conditions> World, are you there? <cfelse> Goodbye World! </cfif>
There can be an unlimited number of <cfelseif> blocks, but a maximum of one <cfelse> block.
Switch Blocks
A <cfswitch> statement is similar to a <cfif> statement, except that it can only check for an equality comparison of a single expression. It cannot, for example, be used to check if one value is higher than another.
<cfswitch expression="#language#"> <cfcase value="German"> Hallo </cfcase> <cfcase value="Spanish"> Hola </cfcase> <cfcase value="French"> Bonjour </cfcase> <cfdefaultcase> Hello </cfdefaultcase> </cfswitch>
Decision and Boolean Operators
ColdFusion use words rather than symbols for its decision and boolean operators.
| Operator Type | Operator | Description |
|---|---|---|
| Decision |
IS EQ EQUAL |
Returns True if the values are equal. |
IS NOT NEQ NOT EQUAL |
Returns True if the values are not equal. | |
GREATER THAN GT |
Return True if the value on the left is greater than the value on the right. | |
LESS THAN LT |
Return True if the value on the right is greater than the value on the left. | |
GREATER THAN OR EQUAL TO GTE GE |
Returns True if the value on the left is greater than or equal to the value on the right. | |
LESS THAN OR EQUAL TO LTE LE |
Returns True if the value on the right is greater than or equal to the value on the left. | |
CONTAINS |
Returns True if the value on the left contains the value on the right. | |
DOES NOT CONTAIN |
Returns True if the value on the left does not contain the value on the right. | |
| Boolean |
NOT |
Reverses the value of an argument. |
AND |
Returns True if both arguments are True. | |
OR |
Returns True if either argument is True. |
Short-circuiting
ColdFusion stops evaluating a Boolean condition as soon as it knows the result. For example, when checking to see if "a AND b" is true, ColdFusion will only look at b if a is true, because if a is false then the whole expression is also false, regardless of the value of b. Likewise, when checking to see if "a OR b" is true, ColdFusion will only look at b if a is false, because if a is true then the whole expression is also true, regardless of the value of b. This behavior is called short-circuiting.
Condition Examples
Following are some examples of if-else if-else and switch-case expressions.
Code Sample: FlowControl/Demos/If.cfm
<html> <head> <title>If-Else If-Else</title> </head> <body> <cfset Age = 21> <cfif Age GTE 21> You can vote and drink! <cfelseif Age GTE 18> You can vote, but can't drink. <cfelse> You cannot vote or drink. </cfif> </body> </html>
The file is relatively simple. You can see the different results by changing the value of Age.
Code Sample: FlowControl/Demos/If2.cfm
<html> <head> <title>If-Else If-Else - Compound</title> </head> <body> <cfset Age = 21> <cfset Citizen = false> <cfif Age GTE 21 AND NOT Citizen> You can drink but you can't vote! <cfelseif Age GTE 21> You can vote and drink! <cfelseif Age GTE 18 AND Citizen> You can vote but can't drink! <cfelse> You cannot vote or drink. </cfif> </body> </html>
The example above shows the use of compound if conditions using boolean operators.
Code Sample: FlowControl/Demos/Switch.cfm
<html> <head> <title>Switch-Case</title> </head> <body> <cfset Quantity = 1> <cfswitch expression="#Quantity#"> <cfcase value="1"> Quantity is 1 </cfcase> <cfcase value="2"> Quantity is 2 </cfcase> <cfdefaultcase> Quantity is not 1 or 2 </cfdefaultcase> </cfswitch> </body> </html>
In many languages, a break is needed to end switch-case processing. In ColdFusion, this is not the case. As soon as the expression is matched, that case block code is executed, and the switch-case block exits.
Redirection Using <cflocation>
The <cflocation> tag is used to redirect the user to a different page. It is commonly used when a page has been moved or when a user doesn't have permissions to view a requested page. The syntax is as follows:
<cflocation url="some_url">
For example:
<cflocation url="http://www.webucator.com">
isDefined() and <cfparam>
ColdFusion errors when it is asked to try to evaluate an undefined variable. In some cases, you will want to check to see whether a variable is defined before trying to evaluate it. You can use the isDefined() function to check this.
<cfif isDefined("Form.firstname")>
<cfoutput>Hello, #Form.firstname#!</cfoutput>
<cfelse>
Hello, stranger!
</cfif>In other cases, you will want to set a default value for a variable in case it is accessed before it is explicitly defined. Use <cfparam> for this.
<cfparam name="Form.firstname" default="stranger"> <cfoutput>Hello, #Form.firstname#!</cfoutput>
In the example above, if Form.firstname is defined before the <cfparam> tag is reached, then the <cfparam> tag would be ignored. Otherwise, the default value of stranger would be assigned to Form.firstname.
Loops
As the name implies, loops are used to loop (or iterate) over code blocks. The following section shows the syntax for some of the different types of loops. All of these loops can be found in FlowControl/Demos/Loops.cfm.
Index Loops
An index loop is like a for loop in other languages. It steps through a process based on some type of count.
<cfloop index="count" from="1" to="5" step="2"> <cfoutput>#count#</cfoutput> </cfloop>
This loop will output 1 3 5.
Conditional Loops
A conditional loop is like a while loop in other languages. It continues to iterate through a process as long as the specified condition is true.
<cfset count=1> <cfloop condition="count LTE 5"> <cfoutput>#count#</cfoutput> <cfset count = count + 2> </cfloop>
This loop will output 1 3 5.
List Loops
Lists are simply strings delimited by a specified character or set of characters. By default, the delimiter is a comma. ColdFusion has many functions for working with lists.
<cfloop> can be used to iterate through a list, performing some function with each list item.
<cfset numlist="1,2,3,4,5"> <cfloop index="num" list="#numlist#"> <cfoutput>#num#</cfoutput> </cfloop> <cfset beatles="paul john ringo george"> <ul> <cfloop index="beatle" list="#beatles#" delimiters=" "> <li><cfoutput>#beatle#</cfoutput></li> </cfloop> </ul>
The first loop will output 1 2 3 4 5. The second loop will output an unordered list of the Beatles' names.
Other Types of Loops
<cfloop> can also be used to loop through other types of data, including queries and structures. Some of these advanced uses of <cfloop> will be covered later in the course.
<cfbreak>
The <cfbreak> tag is used to break out of loops before they have finished processing. The syntax is shown below:
<cfloop index="count" from="1" to="5" step="1"> <cfoutput>#count#</cfoutput> <cfif count GTE 3> <cfbreak> </cfif> </cfloop>
<cfsavecontent>
ColdFusion's <cfsavecontent> tag provides another mechanism for storing data in a variable. The syntax is as follows:
<cfsavecontent variable="var_name"> Variable value </cfsavecontent>
The great thing about <cfsavecontent> is that it can contain other ColdFusion code such as conditionals and loops. This makes it easy to save the results of complicated code as a string of text without appending to a variable with <cfset>.
The following example illustrates the value of <cfsavecontent>.
Code Sample: FlowControl/Demos/cfsavecontent.cfm
<html> <head> <title>cfsavecontent Demo</title> </head> <cfset beatles = ArrayNew(1)> <cfset ArrayAppend(beatles,"John")> <cfset ArrayAppend(beatles,"Paul")> <cfset ArrayAppend(beatles,"George")> <cfset ArrayAppend(beatles,"Ringo")> <body> <h1>Store Beatles as HTML List</h1> <h2>With cfset</h2> <cfset BeatlesList = "<ul>"> <cfloop from="1" to="#ArrayLen(beatles)#" index="i"> <cfset BeatlesList = BeatlesList & "<li>" & beatles[i] & "</li>"> </cfloop> <cfset BeatlesList = BeatlesList & "</ul>"> <cfoutput>#BeatlesList#</cfoutput> <h2>With cfsavecontent</h2> <cfsavecontent variable="BeatlesList2"> <ul> <cfloop from="1" to="#ArrayLen(beatles)#" index="i"> <cfoutput><li>#beatles[i]#</li></cfoutput> </cfloop> </ul> </cfsavecontent> <cfoutput>#BeatlesList2#</cfoutput> </body> </html>
As you can see from the bolded sections, setting and changing values with <cfset> can become ugly and <cfsavecontent> provides a much more elegant solution.
Flow Control Conclusion
In this lesson of the ColdFusion tutorial, you have learned to work with if-else if-else and switch conditionals, to recognize and use ColdFusion loops and to redirect pages with <cflocation>.
