Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Excel, is there an early opt out AND/OR functions (Short Circuit Evaluation)?

In Excel, is there an early opt out AND function (also known as short-circuit evaluation)?

For example:

=AND(FALSE, #N/A)

Returns #N/A. If the function was an "early opt out", it would return FALSE as soon as the first FALSE was found, as no additional value could make the function ever return true.

Does such a function exist in excel?

like image 386
Beachhouse Avatar asked Jun 04 '13 17:06

Beachhouse


People also ask

Does excel short circuit?

Excel does use short-circuit evaluation of IF()s. But ... ... Excel creates a decision tree for calculations—"if Cell A1 changes, then recalculate Cells B2, B5, C3, C6, etc." Nested IF()s can slow down overall Excel performance, as can array formulas, and conditional formatting.

How is short-circuit evaluation implemented?

Short-Circuit Evaluation: Short-circuiting is a programming concept in which the compiler skips the execution or evaluation of some sub-expressions in a logical expression. The compiler stops evaluating the further sub-expressions as soon as the value of the expression is determined.

What is evaluation in Excel?

The Evaluate Formula feature walks you through each argument in a formula to help identify and fix any mistakes. You can also use it to understand complex formulas, seeing how each part of a nested function is calculated to reach the final result.

What do you mean by short-circuit evaluation?

Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first ...


2 Answers

What you're calling "early opt out" is more commonly called "short-circuit evaluation," and is generally a feature of languages in the C/C++/C#/Java family (but, notably, not Visual BASIC).

For Excel formulas, some logical functions practice short-circuit evaluation but some do not. AND does not, as you've found. Neither does OR--if the first argument in an OR expression is true, Excel will still try to evaluate the subsequent arguments.

The solution is to use nested IFs; evaluation of IFs goes step-by-step from outer to inner, branching as necessary, and stopping when there is no further nested formula to be evaluated. This produces the correct short-circuit behavior. So you can write your code above as

=IF(FALSE, IF(ISNA(<address of cell to test for the #N/A state>), TRUE), FALSE)

Another example that may be more clear:

Cell A1 contains the value 1 and cell A2 contains the formula "=1/0",
causing a #DIV/0 error.

Put this formula in A3:
=IF(A1 = 0, IF(A2 = 5, "Never get here"), "Short-circuit OK: A1 <> 0")
like image 141
dodgethesteamroller Avatar answered Sep 17 '22 15:09

dodgethesteamroller


The function you're looking for does not exist in native Excel.

You could however, imitate it, e.g. using IFERROR:

=AND(FALSE,IFERROR(A1,FALSE))

(Work ins 2007 and beyond. In 2003, you need to use =IF(ISERROR(A1),FALSE,A1) instead of IFERROR(A1,FALSE).)

Alternatively, you could build a User Define Function:

Public Function EarlyAnd(var1 As Variant, ParamArray vars() As Variant) As Boolean
    On Error GoTo Finalize
    Dim blnTemp As Boolean
    Dim varNext As Variant

    If Not CBool(var1) Then GoTo Finalize
    For Each varNext In vars
        If Not CBool(varNext) Then GoTo Finalize
    Next

    blnTemp = True

Finalize:
    EarlyAnd = blnTemp

End Function

Place this function in a module in the Visual Basic Editor. Now you can use =EarlyAnd(False,A1) in your Excel.

like image 38
Peter Albert Avatar answered Sep 18 '22 15:09

Peter Albert