Program Flow
The concepts and tags you have learned so far have given you just as taste of the power of Coldfusion, the true programming part of any language comes into play when you start controlling how and which parts of the code will execute given a certain set of circumstances, this is called program flow control, because you are controlling the flow of execution. There are a number of coldfusion tags that deal with flow-control some involve the evaluation of expressions in order to determine which part of the code to execute while another may execute a file on the server cfexecute and yet another redirect the code execution from one template to another cflocation.
In this tutorial we will be focusing on only two concepts, if and switch and their associated tags
The if statement asks a question, and provides one or more possible answers to the question:
if mary is greater than 35 years of age then tell mary she is old otherwise tell mary she is young.
To represent the above word problem as Coldfusion code and making the question generic using variables we have the folowing.
Example:
<cfif age gt 35>
<cfoutput> #firstname# you are OLD!</cfoutput>
<cfelse>
<cfoutput> #firstname# you are young</cfoutput>
</cfif>
See the example in action. See the source code.
Since we are using the same page to capture the user input and process it we need to first protect our code from executing before the information has actually been captured. To do this we wrap our code in the following cfif tag which the first example of flow-control:
<cfif isdefined("form.submit") and form.age neq '' and form.firstname neq ''>
Coldfusion uses what is called short circuit evaluation instead of checking all the expressions and then determining whether or not to continue coldfusion checks the first on determines that the submit button hasn't been pressed, knows that it doesn't need to bother checking any others (since we are using AND instead of OR) and skips to the closing </cfif>. Short circuit evaluation improves performance so it's also important to make sure you put the most likely thing to fail in the front. For this example that means putting the form.submit up front, if we had put any of the other two tests in the front we would have received a Coldfusion error because form.age and form.firstname don't exist until after the submit button has been pressed.
After someone clicks the submit button the code inside the outer cfif is executed if the test passes using the inner cfif (the code above) we determine if the person is old or young, and print out our message as appropriate.
You can also extend the cfif..cfelse combination of tags and include the cfelseif tag, this allows you to further refine the flow of the code.
Example:
<cfif age gt 65>
old geezer!
<cfelseif age lt 65 and age gt 35>
you are getting up there
<cfelse>
young whipper snapper
</cfif>
In the above snippet of code if the user entered an age above 65 old geezer would display, between 36 and 64 they would be told they are getting up there and then below 36 they would be considered a young whipper snapper. You can use an unlimited number of cfelseif tags but the more you use the more difficult to trace and create efficient code. A more efficient approach is to use the cfswitch tag.
CFSWITCH
The cfswitch tag creates a one level "branching" structure. It evaluates an expression and then using one or more child cfcase tags to determine which section of code to execute.
Example:
<cfswitch expression="#form.grade#">
<cfcase value="a">
<cfoutput>#form.firstname#, you ROCK!</cfoutput>
</cfcase>
<cfcase value="b">
<cfoutput>#form.firstname#, great job!</cfoutput>
</cfcase>
<cfcase value="c">
<cfoutput>#form.firstname#, not bad!</cfoutput>
</cfcase>
<cfcase value="d">
<cfoutput>#form.firstname#, nice try</cfoutput>
</cfcase>
<cfcase value="f">
<cfoutput>no comment</cfoutput>
</cfcase>
<cfdefaultcase>
<cfoutput>you didn't enter a grade that I could recognize</cfoutput>
</cfdefaultcase>
</cfswitch>
See it in action. See the source
In the example above the expression in this case is simply a letter grade, the only case statement that will execute is the one that matches the contents of the form variable. While the order of the list doesn't affect which item gets executed there is some thought that in very large switch statement blocks you will get some performance benefit if the most commonly or likely to be executed statements are at the top of the list.