What is the difference between the &
and &&
logical operators in MATLAB?
The single ampersand & is the logical AND operator. The double ampersand && is again a logical AND operator that employs short-circuiting behaviour. Short-circuiting just means the second operand (right hand side) is evaluated only when the result is not fully determined by the first operand (left hand side)
A & B (A and B are evaluated)
A && B (B is only evaluated if A is true)
d != 0 && 1/d
vs d !=0 & 1/d
- the first guarantees no division by zero, the second doesn't. –
Ciborium &
and |
do short-circuit, sometimes. Quoth the documentation: "When you use the element-wise &
and |
operators in the context of an if
or while
loop expression (and only in that context), they use short-circuiting to evaluate expressions." This bizarre behavior is peculiar to MATLAB and is not shared by any other language that uses these operators. –
Ecchymosis &
does short-circuit if in an if
statement. And &&
takes scalar inputs. @Loren's answer below is correct. –
Backwards &&
and ||
take scalar inputs and short-circuit always. |
and &
take array inputs and short-circuit only in if/while statements. For assignment, the latter do not short-circuit.
See these doc pages for more information.
&
and |
in if/while statements? It does not seem to be the case in R2012b and R2014a. –
Vandal As already mentioned by others, &
is a logical AND operator and &&
is a short-circuit AND operator. They differ in how the operands are evaluated as well as whether or not they operate on arrays or scalars:
&
(AND operator) and|
(OR operator) can operate on arrays in an element-wise fashion.&&
and||
are short-circuit versions for which the second operand is evaluated only when the result is not fully determined by the first operand. These can only operate on scalars, not arrays.
Both are logical AND operations. The && though, is a "short-circuit" operator. From the MATLAB docs:
They are short-circuit operators in that they evaluate their second operand only when the result is not fully determined by the first operand.
See more here.
&
is a logical elementwise operator, while &&
is a logical short-circuiting operator (which can only operate on scalars).
For example (pardon my syntax).
If..
A = [True True False True]
B = False
A & B = [False False False False]
..or..
B = True
A & B = [True True False True]
For &&
, the right operand is only calculated if the left operand is true, and the result is a single boolean value.
x = (b ~= 0) && (a/b > 18.5)
Hope that's clear.
bitand
is the bitwise logical AND operator in MATLAB. –
Backwards && and || are short circuit operators operating on scalars. & and | operate on arrays, and use short-circuiting only in the context of if
or while
loop expressions.
A good rule of thumb when constructing arguments for use in conditional statements (IF, WHILE, etc.) is to always use the &&/|| forms, unless there's a very good reason not to. There are two reasons...
- As others have mentioned, the short-circuiting behavior of &&/|| is similar to most C-like languages. That similarity / familiarity is generally considered a point in its favor.
- Using the && or || forms forces you to write the full code for deciding your intent for vector arguments. When a = [1 0 0 1] and b = [0 1 0 1], is a&b true or false? I can't remember the rules for MATLAB's &, can you? Most people can't. On the other hand, if you use && or ||, you're FORCED to write the code "in full" to resolve the condition.
Doing this, rather than relying on MATLAB's resolution of vectors in & and |, leads to code that's a little bit more verbose, but a LOT safer and easier to maintain.
r((r<2)&(r<2))
". –
Atrice © 2022 - 2024 — McMap. All rights reserved.
&
can operate on arrays but&&
can only operate on scalars. – Fluff