On 07/06/2024 11:52, Lawrence D'Oliveiro wrote:
I wonder why, traditionally, shortcut evaluation of boolean subexpressions has been applied to “and” and “or” connectives, but not any others.
Common sense applies, otherwise you could shortcut these operations:
a * b // when a is zero, the result is zero
a & b // when a is zero
Here you would probably spend more time checking the value of 'a' then branching, than just doing the operation.
The language chooses to specify that both operands are always evaluated.
That is the most useful behaviour.
Short-circuiting logical ops are mainly associated with conditional
expressions used for branching, such as in 'if', 'while', 'for' statements.
If both operands always had to be evaluated, then code would be
inefficient as it would need to create an actual result:
if (f() && g())
Not only would both functions be called, but you'd need to combine the
results which means remembering the value of f().
(I have played with a logical and/or operators that didn't have
short-circuit semantics; they worked like this:
if f() andb g()
('andb' means 'and-both'.) But I already had too many such features and eventually it was dropped.
My 'andb' example be trivially expressed in other ways, such as in this C:
if (!!f() & !!g())
)
For example, what about “implies”?
a implies b
What about it? I've never used that, ever. I doubt many have.
If it can be rewritten 'not a or b' then just use that.
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)