Flow control tags exist to control how a program proceeds. Coldfusion uses conditional processing tags to control the flow. This article covers some of the more important flow control tags in the language, as well as script equivalents.
What You Must Know About ColdFusion Flow-Controls - What if, and where to scenarios (Page 2 of 6 )
The conditional processing tag cfif
This decision element is encountered almost everywhere. We are constantly facing the issue, what if? The ColdFusion tag cfif provides this logic for the program to take a certain path based on the outcome. This tag has an attribute as well as a closing end tag. The attribute expression controls the flow. The syntax of this tag is as follows:
---if the expression is true follow the
instruction in this place.---
If the expression is true, the instruction between the tags is followed, or executed, and then only the next line is processed. If the expression is false (not true), it goes directly to the next line in the program. This is illustrated in the next two examples.
The conditional part of the program is highlighted. In the first two lines, two variables are assigned values, an age and a name. The expression that is being evaluated is, #nam# eq "Geoge" . It is clearly seen that the assigned value of nam is George and not Geoge. Hence the expression evaluates to false. Conditional operators were discussed in an earlier tutorial and should be reviewed to determine the evaluated Boolean values of expressions. Since the evaluated value is false, the program proceeds to the next line and processes that line.
<cfif #nam# eq "Geoge">
#nam#'s age is #age#
The displayed output from this code is therefore: Processing finished... On the other hand, if the expression were to be #nam# eq "George", then the expression evaluates to true and the output will be: George's age is 18 Processing finished... Hence no matter what the expression evaluates to, the program proceeds to the next line. Since ColdFusion code can be implemented by tags as well as a script, the following example shows a script based equivalent of the cfif tag.
if (firstName EQ "John")
writeoutput (firstName & "'s age is " & age);
The displayed output for this code after processing is: John's age is 50
The conditional processing tag cfelse
The <cfelse> provides a way to use the <cfif> when its outcome turns out to be false instead of true. It extends the idea of if to include if not, what then?. The usage of <cfelse> is therefore inside a <cfif/> tag. It is the last block inside an <cfif/> block. The syntax of the overall conditional is:
--if expression evaluates to true, process this line of code--
--if expression evaluates to false, process this line of code--
</cfif> Example 3
In this example, the expression #nam# eq "Jon" evaluates to false since the variable nam is "John" and not "Jon". Hence the program moves to the line after the <cfelse> and processes that line.
<cfif #nam# eq "Jon">
#nam#'s age is #age#
This man is not #nam#
Since the line in the <cfelse> tag is processed, the displayed output will be: This man is not John
The next code snippet is the script equivalent of the <cfif/> tag with the additional <cfelse> tag. The expression again evaluates to false and the displayed output will be: We need to know whose age you want?.
age=50; if (firstName EQ "Jon") writeoutput (firstName & "'s age is " & age); else writeoutput ("We need to know whose age you want?");
The conditional processing tag cfelseif
This tag further extends the idea of if to include testing for more than one condition. It is always nested in an <cfif> tag and does not require an end tag. The condition not handled by the <cfif> will be handled by this tag. The syntax for the usage of this tag is as follows;
--if expression1 evaluate to true process this line
otherwise process the next.--
--if expression2 evaluate to true process this line
otherwise process the next. --
--if the above two conditions did not result in true
process this line--
Although the code above tested two conditions, it is possible to set up the code so that several conditions can be tested using a number of <cfelseif> tags leading to complex conditions. However, unless great care is taken, the process may get out of hand and errors could creep in. It is possible to minimize the errors if code is written from the outside towards the more nested parts, since, if none of the inner blocks return true, the outer code will be processed.
This is the script equivalent of the <cfif> <cfelseif><cfelse></cfif> conditional block. As the program flows, the if statement is tested. If the outcome is true, the code in the if block will be processed; otherwise, it will evaluate the expression in the else if condition. If this evaluates to true, the code in the else if block will be processed; otherwise, the code in the cfelse block will be processed.
if (#myState# EQ "NY")
writeoutput ("You are a New Yorker");
else if (#myState# EQ "NJ")
writeoutput ("You are a New Jersey Guy");
WriteOutput ("I don't know where you come from");
Output for three conditions if myState="CT" the output is: I don't know where you come from if myState="NY" the output is: You are a New Yorker if myState="NJ" the output is: You are a New Jersey Guy if myState=" " the output is: I don't know where you come from
This is the CFML tag implementation of a similar conditional block. If you work through the logic with a salary of 4000, you will have to use table 3. Also notice the use of logical operators used in the compound expressions yielding Boolean results.
<cfif #salary# GTE 45000>
For your salary range use Table 1
<cfelseif #salary# LT 45000 AND #salary# GTE 35000>
<b>Use Table 2 for finding your Tax</b>
<b>Use table 3</b>
For an assumed salary of 41000, the output of this code as displayed in the browser is: