Assume we want to evaluate the Boolean value of
(A || B || C) which is logically equal to
!(!A && !B && !C).
Would it be more time-efficient to use
!(!A && !B && !C) than the other if (!A) is false since the whole expression value becomes false? Will the program notice that? Or the program still needs to evaluate the value of (!B) and (!C) before returning the final result?
P.S. Just correcting the de'Morgan expressions and change from
!. Thanks guys.
(A || B || C) is logically equal to
!(!A && !B && !C), you were missing the inner
!s. The short circuiting in the second expression happens when
false, or equivalently when
true. But when
true, the first expression also short-circuits. In other words, these two expressions behave the same way with respect to short circuiting.
As Retired Ninja mentions in a comment, the two expressions produce the same assembly code, so there is no difference at all between them, except that one is harder to read.