Using IF Statements

Team-Fly    

ColdFusion® MX: From Static to Dynamic in 10 Steps
By Barry Moore
Table of Contents
Step 4.  Controlling Program Flow


The IF statement is a fundamental feature of just about every programming language. It is used to check for a particular condition and then perform one of several actions, depending on the outcome.

This is not as complex as it sounds. People use IF statements everyday; we just don't notice. My father was great at it. Every time I wanted to borrow the car as a teenager, he would say, "If you dent this car, I will kick your backside."

He was checking for a certain conditionwhether the car was dentedand then performing an action if that condition were truekicking my backside.

As you might have picked up from the previous section, ColdFusion uses the <CFIF> tag to create these type of IF statements.

Using <CFIF>

The <CFIF> tag contains no attributes; rather, it contains a comparison of some sort. The <CFIF> tag is also a container tag, so it requires a closing </CFIF> tag. If the comparison we are performing returns a value of TRUE, the code between the <CFIF> tags is executed. If the comparison returns a value of FALSE, the code is skipped, and the rest of the template executes as normal. So, if my father had been a ColdFusion programmer, he would have come up with the following block of code:

 <CFIF CarDented IS "YES">        <!--- code to automate the kicking of Barry's backside--->  </CFIF>

If I had brought the car home with a dent in it, the automated backside-kicking code would have swung into action, but if CarDented IS "NO" (there was no dent), nothing would have happened and I could have just gone along my merry way.

Using <CFELSE>

If we want to run two bits of code, one if the condition we are testing for is met and another if it is not, we can use the <CFELSE> tag. The <CFELSE> tag must be placed between the opening and closing <CFIF> tags. For example, even when I brought home Dad's car dent free, that was not usually the end of the story. The following code shows what usually happened:

 <CFIF CarDented IS "YES">    <!--- code to automate the kicking of Barry's backside--->  <CFELSE>    Eye Barry suspiciously and check the quantity of fuel in the gas tank  </CFIF>  continue on with other code

The code between the <CFELSE> tag and the closing </CFIF> is executed if the CarDented IS "YES" condition evaluates to FALSE (the car is not dented).

In the preceding code block, if CarDented IS "YES", I would have gotten my backside kicked. We would have skipped over the <CFELSE> portion of the code block and continued on just after the closing </CFIF> tag. However, even if the car was not dented (CarDented IS "NO"), the old man still would have given me the evil eye and checked the gas tank.

This type of IF statement is very useful for things like security and personalization in your web site. For example, you can use it to check to see whether a user is logged in and either to grant the person access or redirect him to a login page.


    Team-Fly    
    Top
     



    ColdFusion MX. From Static to Dynamic in 10 Steps
    ColdFusion MX: From Static to Dynamic in 10 Steps
    ISBN: 0735712964
    EAN: 2147483647
    Year: 2002
    Pages: 140
    Authors: Barry Moore

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net