Skip to main content
Inspiring
April 8, 2011
Question

Simple conditional question

  • April 8, 2011
  • 2 replies
  • 683 views

Many times I got confuse when it comes to using cfif and cfelse.

When I only need to compare 1 variable, it is very clear. If VariableA is blank, count else do not count. Else in here clearly indicates if VariableA is NOT blank.

But in situation such as the following, I'm not sure what cfelse refering to:

<CFLOOP query="MyQuery">

  <!--- In cases where either one of VariableA.B or C is Blank/Doesn't have value --->

   <CFIF (#Len(VariableA)# EQ 0) OR (#Len(VariableB)# EQ 0) OR (#Len(VariableC)# EQ 0)>

     .......

   <CFELSE>

     ......

   </CFIF>

</CFLOOP>

What I want in CFELSE above is, either VariableA AND B AND C can NOT be blank

So, Is the CFELSE here means : A1.  (#Len(VariableA)# NEQ 0) OR  (#Len(VariableB)# NEQ 0) OR  (#Len(VariableC)# NEQ 0) or

                                                 A2.  (#Len(VariableA)# NEQ 0) AND  (#Len(VariableB)# NEQ 0) AND  (#Len(VariableC)# NEQ 0)

This topic has been closed for replies.

2 replies

Inspiring
April 8, 2011

In addition to the other answer, I think you are processing your logic in the wrong order.  I would test that at least one of them had a value before checking to see if one of them was blank.

One way to do that is to add the lengths to each other.

Inspiring
April 8, 2011

VariableA is for City, B is for State and C is for ZipCode

Another variable I did not mention in here is ForeignCityZip, which I use to deffirentiate between US and Foreign location

So my logic goes like this:

If ForeignCityZip is blank,

    This is US location and US location need City,State and ZipCode

       If either one of this column is blank, error out !

         So this is the reason why I use:

           <CFIF Len(VariableA) EQ 0 OR Len(VariableB) EQ 0 or Len(VariableC) EQ 0>

                  .....

           <CFELSE>

           In here I need : City and state and Zip all need to have values so I can use this valid data

           for further coding       

                 -----

          </CFIF>

ilssac
Inspiring
April 8, 2011

I would turn the logic around.

<CFIF Len(VariableA) NEQ 0 AND Len(VariableB) NEQ 0 AND Len(VariableC) NEQ 0>

    <!--- ALL THE VARIABLES HAVE VALUES, process them --->

<CFELSE>

   <!--- One or more of the variables DO NOT have values, throw the error --->

</CFIF>

Message was edited by: ilssac  EDITED to add the NOT EQUAL operator required for this version of the Boolean logic.

ilssac
Inspiring
April 8, 2011

First of all, you do not need all those hash|pound|number|octothorpe|# symbols.  You only need those when you want to render the value of a variable into string output.

Secondly, using OR's mean that the true clause will be used if any one or more of the variables have a length.  Using AND's mean that the true clause will be used only if ALL the varaibles have a length.

Of course, this means that the false (aka ELSE) clause will be the oposite in each case.  With the OR's, that means the ELSE will only be used if ALL the variables have no length.  With the AND's, the ELSE clause will be used if any one or more of the variables has no length.