Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nesting _IFF
#1
Doesn't appear to be addressed in the wiki or KotD, but how do you nest _IIF or is that's not possible, but if it is, how deep can it be nested?
Reply
#2
You can. For example:

Code: (Select All)
age = 16

PRINT _IIF(age < 13, "child", _IIF(age < 20, "teenager", _IIF(age < 65, "adult", "senior")))

But I have never checked how deep though. I'd suggest not overdoing it. IMO, more than 2 levels makes a code hard to read.
Reply
#3
(02-12-2026, 10:29 PM)a740g Wrote: You can. For example:

Code: (Select All)
age = 16

PRINT _IIF(age < 13, "child", _IIF(age < 20, "teenager", _IIF(age < 65, "adult", "senior")))

But I have never checked how deep though. I'd suggest not overdoing it. IMO, more than 2 levels makes a code hard to read.

If your code is getting to this point and depth of IIFing, then I'd IIFing just use SELECT CASE.  It's much more readable to me.

Code: (Select All)

SELECT CASE age
    CASE <13: PRINT "child"
    CASE <20: PRINT "teenager"
    CASE <65: PRINT "adult"
    CASE ELSE: PRINT "senior"
END SELECT

One glance at the above and I can decipher what the heck it's doing with no thought or brain power really needed. 

That compound IFF statement?  It's not intuitive.  I'd have to stop and take a moment to wrap my brain around it, and that makes it much less viable as *GOOD* code, in my opinion.

Just because you CAN do something doesn't mean you really should.  It's often better to just stick to the simple constructs which you (and everyone else) recognize and understand inherently.  It makes sharing, upkeeping, and maintaining code a helluva lot easier over time.
Reply
#4
Thanks for the info. I'm finding I really like a lot of the new logic operators and I read somewhere that _IIF could replace If..Then..Else but it appears _IIF can not handle multiple blocks like Elseif (unless we come up with an _ElseIIF). Truthfully I gave up on IF..THEN.. ELSEIF in favour of Select Case. And as I understand it, _IIF is restricted to True/False, it can't for example call a subroutine if the condition is met - ie result = (_IIF(age<13,Child,Adult). We would then need a further IF statement ( ie If result = Child then Underage). to call a subroutine to deal with Children. 

And I hear the groans ... "Gave them a new toy and they broke it already". I got to go back to that article and re-read it.

Again, thanks for the discussion.
Reply
#5
I agree with Steve, the only advantage of IIF is that you can use it in an expression but even then it's harder to mentally process than a simple if then else
Reply
#6
I've used compound _IIF statements, and I agree the readability is somewhat poor. That said, I was using a lot of branchless equations and minimizing variables as a coding style and they were far worse to read. It seems to me that if you are looking for short circuiting decisions in complex expressions, then _IIF is somewhat better than my old methods. At least for readability.

Here's a nightmare expression
vl% = (MinOf%(vl% + ch%, 5) * (bs% > 0)) * (md% = 0) + ((-MinOf%(vl% + ch%, 5)_
            * (vl% > 5) - MinOf%(vl% + ch%, vl%) * (vl% <= 5)) * (bs% = 0) - MinOf%(vl% + ch%, 5) * (bs% > 0)) * (md% > 0)

It did exactly what it was supposed to do, but try to wrap your brain around it. Wink The MinOf% function was my homebrewed _MAX command. I suspect that _IIF would be a piece of cake compared to it.
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply
#7
Something's still missing!  Big Grin

Code: (Select All)

Dim As Integer age

Print
Input "Alter: ", age

Print
Select Case age
  Case Is = 0: Print "Coming soon! Keep cool!"
  Case Is < 13: Print "child"
  Case Is < 20: Print "teenager"
  Case Is < 65: Print "adult"
  Case Else: Print "senior"
End Select

End
Reply
#8
Haha.  Case Is > 95: Print "Very, very senior"

You must mean that, right?  Wink
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)