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 ~
to !
. Thanks guys.
P.S.2 shown in this link by Retired Ninja and optimized version by David Foerster. The assembly code is actually the same!
You can have multiple Boolean Operators in one statement. It doesn't matter that there are multiple statements. Each statement must be true in order for the whole to evaluate to True .
As we know, a variable of the type boolean can take on only two values: true and false.
Named after George Boole (1815-1864), who invented mathematical logic and defined Boolean algebra. A variable of the primitive data type boolean can have two values: true and false (Boolean literals). or off. Boolean expressions use relational and logical operators.
A Boolean expression is a Java expression that returns a Boolean value: true or false .
(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 !A
is false
, or equivalently when A
is true
. But when A
is 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With