Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: bart Newsgroups: comp.lang.c Subject: Re: Shortcut Booleans Date: Fri, 7 Jun 2024 13:41:06 +0100 Organization: A noiseless patient Spider Lines: 51 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 07 Jun 2024 14:41:05 +0200 (CEST) Injection-Info: dont-email.me; posting-host="29f6a6cdd40af68cde491d55d42bbb4c"; logging-data="2173435"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+6Z3Duk0sXamP/ud2x/hzE" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:cIbE79W2rAYKJJR01rVIhdl/2nM= In-Reply-To: Content-Language: en-GB Bytes: 2534 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.