ColdFusion 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:
Tags |
Purpose |
---|---|
cfif, cfelseif, cfelse |
Select sections of code based on whether expressions are True or False. |
cfswitch, cfcase, cfdefaultcase |
Select among sections of code based on the value of an expression. Case processing is not limited to True and False conditions. |
cfloop, cfbreak |
Loop through code based on any of the following values: entries in a list, keys in a structure or external object, entries in a query column, an index, or the value of a conditional expression. |
cfabort, cfexit |
End processing of a ColdFusion page or custom tag. |
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 cfelse
The cfif, cfelseif, and cfelse tags provide if-then-else conditional processing, as follows:
- The cfif tag tests a condition and executes its body if the condition is True.
- If the preceding cfif (or cfelseif) test condition is False, the cfelseif tag tests another condition and executes its body if that condition is True.
The cfelse tag can optionally follow a cfif tag and zero or more cfelseif tags. Its body executes if all the preceding tags' test conditions are False.The following example shows the use of the cfif, cfelseif, and cfelsetags. If the value of the type variable is "Date," the date displays; if the value is "Time," the time displays; otherwise, both the time and date display.