MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. An operator is a symbol that operates on a value to perform specific mathematical or logical computations. They form the foundation of any programming language. Here we see & and && operator in MATLAB.
- & Operator: It is a logical AND operator but it does not employ Logical Short Circuiting Behaviour.
- && Operator: It is also a logical AND operator which works on Logical Short circuiting Behaviour.
Now we see examples for both operators.
Example 1.
% MATLAB code
a = 5;
b = 10;
% True & false = false
x = (a-b) < 0 & (a/b) < 0;
disp(x);
% True && true = true
y = (a-b) < 0 && (a/b) > 0;
disp(y);
% Second expression won't be checked because 1st expression is false
z = (a-b) > 0 && (a/b) > 0;
disp(z);
Output:

In the above example, we are declaring two variables a = 5 and b = 10. Now for x, we are using & operator. Here expression A is (a-b) < 0 which is true and expression B is (a/b) < 0 which is false. The logical AND of both expressions will be considered and the output will be stored in x. Here x will store False(0) because the Logical AND of true and false is False.
Now for y, we are using && operator. Here expression A is (a-b) < 0 which is true. So expression B will be considered while calculating y. Expression B is (a/b) > 0 which is true. The logical AND of both expressions will be considered and the output will be stored in y. Here y will store True(1) because the Logical AND of true and true is True.
Now for z, we are using && operator. Here expression A is (a-b) > 0 which is false. So expression B will not be considered while calculating z. Here x will store False(0) because the expression A is false.
Example 2:
% MATLAB code
a = 100;
b = 10;
% true & true = true
x = (a-b) > 0 & (a/b) > 0;
disp(x);
% true && true = true
y = (a-b) > 0 && (a/b) > 0;
disp(y);
% second expression won't be checked
% because 1st expression is false
z = (a-b) < 0 && (a/b) > 0;
disp(z);
Output:

Difference Between & Operator and && Operator:
& Operator | && Operator |
|---|---|
It is a logical AND operator but it does not employ Logical Short Circuiting Behaviour. | It is also a logical AND operator which works on Logical Short circuiting Behaviour. |
expressionA & expressionB When we use &, both the expressions will be evaluated and then logical AND will be the output. | expressionA && expressionB When we use &&, expression B will be evaluated only if expression A is true. |
If both expression A and expression B are true, the output is True otherwise it is false. | If expression A is false, the output is false. If it is true the output is the logical AND of both expressions. |
None of the expressions short circuits. | Short-circuiting takes place if expression A is false. |