ColdFusion 9.0 Resources |
Flow controlColdFusion provides several tags that let you control how a page gets executed. These tags generally correspond to programming language flow control statements, such as if, then, and else. The following tags provide ColdFusion flow control:
CFScript also provides a set of flow-control statements. For information on using flow-control statements in CFScript, see Extending ColdFusion Pages with CFML Scripting. For more details on using flow-control tags, see the reference pages for these tags in the CFML Reference. cfif, cfelseif, and cfelseThe cfif, cfelseif, and cfelse tags provide if-then-else conditional processing, as follows:
cfswitch, cfcase, and cfdefaultcaseThe cfswitch, cfcase, and cfdefaultcase tags let you select among different code blocks based on the value of an expression. ColdFusion processes these tags as follows:
Note: Although the cfdefaultcase tag does not
have to follow all cfcase tags, it is good programming
practice to place it at the end of the cfswitch statement.
The cfswitch tag provides better performance than a cfif tag with multiple cfelseif tags, and is easier to read. Switch processing is commonly used when different actions are required based on a string variable such as a month or request identifier. The following example shows switch processing:
<cfoutput query = "GetEmployees"> <cfswitch expression = #Department#> <cfcase value = "Sales"> #FirstName# #LastName# is in <b>Sales</b><br><br> </cfcase> <cfcase value = "Accounting"> #FirstName# #LastName# is in <b>Accounting</b><br><br> </cfcase> <cfcase value = "Administration"> #FirstName# #LastName# is in <b>Administration</b><br><br> </cfcase> <cfdefaultcase>#FirstName# #LastName# is not in Sales, Accounting, or Administration.<br> </cfdefaultcase> </cfswitch> </cfoutput> cfloop and cfbreakThe cfloop tag loops through the tag body zero or more times based on a condition specified by the tag attributes. The cfbreak tag exits a cfloop tag. cfloopThe cfloop tag provides the following types of loops:
The following example shows a simple index loop: <cfloop index = "LoopCount" from = 1 to = 5> The loop index is <cfoutput>#LoopCount#</cfoutput>.<br> </cfloop> The following example shows a simple conditional loop. The code does the following:
Note: You can get an infinite conditional loop if
you do not force an end condition. In this example, the loop is
infinite if you omit the <cfset i = i + 1> statement. To
end an infinite loop, stop the ColdFusion application server.
cfbreakThe cfbreak tag exits the cfloop tag. You typically use it in a cfif tag to exit the loop if a particular condition occurs. The following example shows the use of a cfbreak tag in a query loop: <cfloop query="fruitOrder"> <cfif fruit IS "kumquat"> <cfoutput>You cannot order kumquats!<br></cfoutput> <cfbreak> </cfif> <cfoutput>You have ordered #quantity# #fruit#.<br></cfoutput> </cfloop> cfabort and cfexitThe cfabort tag stops processing of the current page at the location of the cfabort tag. ColdFusion returns to the user or calling tag everything that was processed before the cfabort tag. You can optionally specify an error message to display. You can use the cfabort tag as the body of a cfif tag to stop processing a page when a condition, typically an error, occurs. The cfexit tag controls the processing of a custom tag, and can only be used in ColdFusion custom tags. For more information see, Terminating tag execution and the CFML Reference. |