f

#### && and &

```What exactly is the difference between && and & (|| and |)? When does
it generates different results? Is the & wrong now from R14 on?

Best regards
Prom
```
 0
joabu1 (13)
1/31/2006 11:16:33 AM
comp.soft-sys.matlab 211265 articles. 25 followers. lunamoonmoon (257) is leader.

6 Replies
1379 Views

Similar Articles

[PageSpeed] 26

```Prom wrote:
> What exactly is the difference between && and & (|| and |)? When does
> it generates different results? Is the & wrong now from R14 on?
>
> Best regards
> Prom

'help &' shows:

Logical operators.
Short-circuit logical AND         &&
Short-circuit logical OR          ||
and        - Element-wise logical AND           &
or         - Element-wise logical OR            |
not        - Logical NOT                        ~
xor        - Logical EXCLUSIVE OR
any        - True if any element of vector is nonzero
all        - True if all elements of vector are nonzero

Jeroen

```
 0
no_mail860 (236)
1/31/2006 11:15:34 AM
```Hi,

see "Short circuit operators" there :

<http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/ch12_n11.html>

J�r�me
```
 0
dutmatlab (3797)
1/31/2006 11:22:47 AM
```Prom:

> What exactly is the difference between && and &...

doc relop;

us
```
 0
us1 (8051)
1/31/2006 11:24:24 AM
```Jeroen wrote:
>
>
> Prom wrote:
>> What exactly is the difference between && and & (|| and |)?
When
> does
>> it generates different results? Is the & wrong now from R14 on?
>>
>> Best regards
>> Prom
>
> 'help &' shows:
>
> Logical operators.
> Short-circuit logical AND &&
> Short-circuit logical OR ||
> and - Element-wise logical AND &
> or - Element-wise logical OR |
> not - Logical NOT ~
> xor - Logical EXCLUSIVE OR
> any - True if any element of vector is nonzero
> all - True if all elements of vector are nonzero
>
> operators'->'short-circuit'
>
> Jeroen

So it should not be a difference between these two operations, the &&
is 'only' a little bit faster, e.g. in case of multiple coupled
logical terms (A && B && C && D && E ...)?

Greetings Prom
```
 0
joabu1 (13)
1/31/2006 11:43:00 AM
```Often yes, but there can of course be a real difference if B, C, D, E
etc. are functions having side-effects, e.g. altering global variables
or doing something in the "real world"...

David

```
 0
1/31/2006 11:52:51 AM
```PROM wrote:
> Jeroen wrote:
>>
>> Prom wrote:
>>> What exactly is the difference between && and & (|| and |)?
> When
>> does
>>> it generates different results? Is the & wrong now from R14 on?
>>>
>>> Best regards
>>> Prom
>> 'help &' shows:
>>
>> Logical operators.
>> Short-circuit logical AND &&
>> Short-circuit logical OR ||
>> and - Element-wise logical AND &
>> or - Element-wise logical OR |
>> not - Logical NOT ~
>> xor - Logical EXCLUSIVE OR
>> any - True if any element of vector is nonzero
>> all - True if all elements of vector are nonzero
>>
>> operators'->'short-circuit'
>>
>> Jeroen
>
> So it should not be a difference between these two operations, the &&
> is 'only' a little bit faster, e.g. in case of multiple coupled
> logical terms (A && B && C && D && E ...)?
>
> Greetings Prom

and sometimes its convenient to avoid errors. with && you can do
something like

if ~isempty(c) && c(2)==5

without the && and c{3} would be empty you'd get an error. in times
before && you had to nest the if statements.

michael

```
 0
themiwi (854)
1/31/2006 12:17:36 PM