Re: Shortcut Booleans

Liste des GroupesRevenir à l c 
Sujet : Re: Shortcut Booleans
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.c
Date : 07. Jun 2024, 14:41:06
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v3uv51$22afr$2@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
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.

Date Sujet#  Auteur
7 Jun 24 * Shortcut Booleans6Lawrence D'Oliveiro
7 Jun 24 `* Re: Shortcut Booleans5bart
8 Jun 24  `* Re: Shortcut Booleans4Lawrence D'Oliveiro
9 Jun 24   `* Re: Shortcut Booleans3David Brown
10 Jun 24    `* Re: Shortcut Booleans2Lawrence D'Oliveiro
10 Jun 24     `- Re: Shortcut Booleans1Keith Thompson

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal