If Statements

Don’t flip out on the syntax of if statements in ColdFusion. It is different than most languages. Well, the conditional is different than most languages. It doesn’t use the typical equal sign. Instead, it uses keywords like NEQ, which stands for not equal to. The if tag begins with <cfif> and ends with </cfif> like any normal tag in ColdFusion. We will get to the else ifs and else at the bottom.

Comparison Operators

Comparison operators in ColdFusion are pretty different from most programming languages. Normally, you would see an if statement with a == which means if they thing before the == is equal to the thing after it. ColdFusion has a bit more verbiage about the comparison operators than the others. You might freak out when you see the EQ, NEQ, GT, GTE, LT, and LTE, but don’t. It isn’t as complicated as it looks.

  • EQ – equal to
  • NEQ – not equal to
  • GT – greater than
  • GTE – greater than or equal to
  • LT – less than
  • LTE – less than or equal to
<cfset x = 3>
<cfset y = 4>
    <cfif x EQ y>
        If x is equal to y <br/>
    <cfif x NEQ y>
        If x is not equal to y <br/>
    <cfif x GT y>
        If x is greater than y <br/>
    <cfif x GTE y>
        If x is greater than or equal to y <br/>
    <cfif x LT y>
        If x is less than y <br/>
    <cfif x LTE y>
        If x is less than or equal to y <br/>

Multiple Conditions

You can add multiple conditions to meet in a conditional. By putting AND between the end of one condition and the beginning of the next condition, you can make sure that both conditions are matched before executing the statements inside the if statement. So, ColdFusion doesn’t use the standard “&&” and will get angry if you try to use it. Also, you can use OR in the exact same manner. Of course, OR would allow the statements to be executed inside of the if statement if any of the conditions around the OR were met. Again, ColdFusion grumbles when you try to use the typical || when you mean OR.

Else if and Else

I almost forget to mention two crucial parts of an if statement. The <cfelseif> and the <cfelse>. The <cfelseif> and <cfelse>, do not actually need closing tags; the next <else> if would actually close the first <cfelseif>, and the <cfelse> would close the <cfelseif> before it.

    <cfset x = 5>
    <cfif x EQ 3>
        <p>cfif was triggered.<p>
    <cfelseif x EQ 5> 
        <p>cfelseif was triggered.<p>
        <p>cfelse was triggered.<p>

Woo! ColdFusion spit out exactly what we wanted. We had a variable x that was equal to 5. So, we went through the first if <cfif x EQ 3> that basically says if our variable x is equal to 5. It’s not, which moves us on to the next part of the if statement <cfelseif x EQ 5>. Now, the else if here is actually true because our variable x does equal 5. Now, that we have found a match we do not proceed to the <cfelse> because of statement has been satisfied. Other than a few odd syntax changes ColdFusion conditionals are not that difficult.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.