# short circuit & and | in if and while

6 messages
Open this post in threaded view
|

## short circuit & and | in if and while

 Is there anyone who can explain me, why in matlab, and thus in octave, if(a&b) is interpreted like if(a&&b), whereas c=a&b differs from c=a&&b which means that if(a&b) is not the same as c=a&b;if(c)... Very strange. This feature also applies to while but not to until... Who can explain me???
Open this post in threaded view
|

## Re: short circuit & and | in if and while

 On Thu, Sep 14, 2017 at 02:15:16 +0200, Ernst Reissner wrote: > Is there anyone who can explain me, > > why in matlab, and thus in octave, > > if(a&b) is interpreted like if(a&&b), > > whereas c=a&b differs from c=a&&b > > which means that > > if(a&b) is not the same as c=a&b;if(c)... > > Very strange. > > This feature also applies to while but not to until... > > Who can explain me??? Here is an official answer from Mathworks Support that describes the behavior   https://www.mathworks.com/matlabcentral/answers/99518-is-the-logical-operator-in-matlab-a-short-circuit-operatorThis is a "what", not a "why". I don't know who you can ask for an answer to "why", it just is. -- mike signature.asc (849 bytes) Download Attachment
Open this post in threaded view
|

## Re: short circuit & and | in if and while

 Yes, Mike, I know the what, but not the why and I really cannot figure out a reason. The reason why I ask is among other things, what about until in octave: Maybe short circuiting should be also for until, as it is for while and if: while (false & error("both")) "hi";end shortcuts and thus no error while do "hi" until(true |error("both")); yields and error. This seems a little inconsistent, but on the other hand, ... what sense makes the matlab feature at all?! > On Thu, Sep 14, 2017 at 02:15:16 +0200, Ernst Reissner wrote: >> Is there anyone who can explain me, >> >> why in matlab, and thus in octave, >> >> if(a&b) is interpreted like if(a&&b), >> >> whereas c=a&b differs from c=a&&b >> >> which means that >> >> if(a&b) is not the same as c=a&b;if(c)... >> >> Very strange. >> >> This feature also applies to while but not to until... >> >> Who can explain me??? > Here is an official answer from Mathworks Support that describes the > behavior > >   https://www.mathworks.com/matlabcentral/answers/99518-is-the-logical-operator-in-matlab-a-short-circuit-operator> > This is a "what", not a "why". I don't know who you can ask for an > answer to "why", it just is. >
Open this post in threaded view
|

## Re: short circuit & and | in if and while

 Administrator On 09/13/2017 09:25 PM, Ernst Reissner wrote: > Yes, Mike, I know the what, but not the why and I really cannot figure > out a reason. > The reason why I ask is among other things, what about until in octave: > Maybe short circuiting should be also for until, as it is for while and if: > while (false & error("both")) "hi";end shortcuts and thus no error while > do "hi" until(true |error("both")); yields and error. > > This seems a little inconsistent, > but on the other hand, ... what sense makes the matlab feature at all?! It doesn't make much sense. Have you read    http://wiki.octave.org/FAQ#Short-circuit_.26_and_.7C_operatorsOriginally in Octave && was equivalent to &, and || was equivalent to |.   Neither form did short-circuiting. I changed the && and || operators (but not the & and | operators) to do short-circuiting in 1994, apparently.  Here is the message I posted then:    http://lists.gnu.org/archive/html/help-octave/1994-05/msg00012.htmlWhen TMW introduced short-circuit behavior for the & and | operators in Matlab, they thought they were being clever by not introducing new operators, and just making the existing ones do something different in some contexts.  To me, this seemed like a really bad choice and I could immediately see cases where behavior would be different depending on whether the expression appeared directly inside and IF/WHILE condition or not. The Matlab behavior is highly unusual and causes trouble in various ways (some documented in the Octave FAQ).  We have it for compatibility ONLY.   Using this feature is STRONGLY discouraged.  I wish to limit the damage, so I have no interest in extending the behavior to other places in the language.  Supporting this feature for compatibility is bad enough. jwe