Skip to main content
April 18, 2006
Answered

IIF function

  • April 18, 2006
  • 3 replies
  • 525 views
Hi,

I'd be grateful if somebody could please help me out with this problem.

I actually want to write the following using the IIF statement:

<cfif temp gt 0>
<cfset label = "Over">
<cfelseif temp lt 0>
<cfset label = "Under">
<cfelse>
<cfset label = "-">
</cfif>

Note
If I output the variable label using the cfif, cfelseif and cfelse, it gives me the correct output, i.e. either "-", Over, or Under.


I tried the following to rewrite the above statement using the iif function:

<cfset label = iif(temp eq 0,DE("-"),(iif(temp gt 0,DE("Over"),DE("Under"))))>

Now, when temp eq 0, it works fine.
However, when temp is lt 0, it throws me an error message saying variable Under is undefined.
And when temp is gt 0, I get the same error message, but in this case it says variable Over is undefined.

However, if I replace Over and Under by 1 or 0 (i.e. integers) it works perfect!

Any idea how to get the output either Over or Under using the iif statement pls?

Thanks and regards,
Yogesh Mahadnac.
This topic has been closed for replies.
Correct answer TSB
<cfset label = iif(temp eq 0,DE("-"),DE(iif(temp gt 0,DE("Over"),DE("Under"))))>

You needed a DE before your second iif.

Trevor
Don't Click Here

3 replies

TSBCorrect answer
Inspiring
April 18, 2006
<cfset label = iif(temp eq 0,DE("-"),DE(iif(temp gt 0,DE("Over"),DE("Under"))))>

You needed a DE before your second iif.

Trevor
Don't Click Here
April 19, 2006
Thanks a lot TSB!

That did the trick.

Cheers!

Yogesh
Inspiring
April 18, 2006
> I actually want to write the following using the IIF statement:

Not that you asked, but IMO this is a mistake, for the very reason you're
experiencing here: the iif() version of this code is quite difficult to
read (and easy to muck up, as you have).

WHY are you wanting to change it to an iif() expression?

> <cfset label = iif(temp eq 0,DE("-"),(iif(temp gt 0,DE("Over"),DE("Under"))))>

I *suspect* (strongly ~) this is because you are "escaping" the overs and
unders in the nested iff() call, but not in the result of the outer one.
So, if temp happens to be greater than zero, for all intents and purposes
you are left with this line of code:

iif(temp eq 0,DE("-"), "Over")

If this is the case, I think you need to have a DE() around you entire
second iif() expression.

BLEAH.

Stick with the CFML

(just opinion).

--
Adam