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:
<cfif expression>
---if the expression is true follow the
instruction in this place.---
</cfif>
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.
Example 1The 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.
<cfset age=18>
<cfset nam="George">
<cfoutput>
<cfif #nam# eq "Geoge">
#nam#'s age is #age#
</cfif>
</cfoutput>
<cfoutput>
Processing finished...
</cfoutput>
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.
Example 2This example shows how to write a conditional processing statement in <cfscript/> which is equivalent to the <cfif/>. This is very similar to what one uses in JavaScript for an if statement. The parenthesis after if contains the expression whose evaluated Boolean value determines what to do next.
<cfscript>
firstName="John";
age=50;
if (firstName EQ "John")
writeoutput (firstName & "'s age is " & age);
</cfscript>
The displayed output for this code after processing is:
John's age is 50
The conditional processing tag cfelseThe <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:
<cfif expression>
--if expression evaluates to true, process this line of code--
<cfelse>
--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.
<cfset nam="John">
<cfset age=50>
<cfoutput>
<cfif #nam# eq "Jon">
#nam#'s age is #age#
<cfelse>
This man is not #nam#
</cfif>
</cfoutput>
Since the line in the <cfelse> tag is processed, the displayed output will be:
This man is not John
Example 4The 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?.
<cfscript>
firstName="John";
age=50;
if (firstName EQ "Jon")
writeoutput (firstName & "'s age is " & age);
else
writeoutput ("We need to know whose age you want?");
</cfscript>
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;
<cfif expression1>
--if expression1 evaluate to true process this line
otherwise process the next.--
<cfelseif expression2>
--if expression2 evaluate to true process this line
otherwise process the next. --
<cfelse>
--if the above two conditions did not result in true
process this line--
</cfif>
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.
Example 5This 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.
<cfscript>
myState="CT";
if (#myState# EQ "NY")
writeoutput ("You are a New Yorker");
else if (#myState# EQ "NJ")
writeoutput ("You are a New Jersey Guy");
else
WriteOutput ("I don't know where you come from");
</cfscript>
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
Example 6
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.
<cfset salary=4000>
<cfoutput>
<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>
<cfelse>
<b>Use table 3</b>
</cfif>
</cfoutput>
For an assumed salary of 41000, the output of this code as displayed in the browser is:
Use Table 2 for finding your Tax
Next: Which way to go? >>
More ColdFusion Articles
More By Jayaram Krishnaswamy