Forum Discussion
IFS evaluates all elements
I noticed this same problem. Thank you for articulating it. I was very happy to have learned about the IFS statement, since it cleaned up formulas quite a bit, but when writing a sudoku solver LAMBDA that recursively looks for new logical values to fill in each recursion loop, I found that I was forced to nest my IF statements, or the program froze (endless looping I presume). I would love an IFS that ceased to check subsequent conditions after the first satisfied condition was found.
I thought about how I would design a BetterIFS recursive lambda function that moved on to the next condition if one wasn't met, but this would require passing all the conditions back to the function... thus evaluating their validity. Then I thought perhaps under the IFS hood is a giant nest of IF statements 127 levels deep! (the limit as per the https://support.microsoft.com/en-us/office/ifs-function-36329a26-37b2-467c-972b-4a39bd951d45), but such a construction still evaluates all the parameters passed to it:
I think I too will steer clear of the IFS statement to avoid errors and unintended evaluations.
- JoeUser2004Mar 23, 2023Bronze Contributor
Timothy Calford wrote: ``I thought perhaps under the IFS hood is a giant nest of IF statements 127 levels deep! [...], but such a construction still evaluates all the parameters passed to it: [....] I think I too will steer clear of the IFS statement to avoid errors and unintended evaluations``
Good conclusion. I would use IFS to "clean up" syntax only when the side-effect of evaluating all parameters before selection seems minimal.
I don't know anything about LAMBA. But ostenibly, your LAMBA construction with nested IFs should indeed evaluate left-to-right as-needed. It would, if the IF construct were outside a LAMBA construct.
So, the behavior that you seed is a side-effect of the LAMBA design. Sad. But good to know. People should be wary of the inherent inefficiencies of some of these new constructs. IMHO, they should be used sparingly, only when they truly are the best solution.