Logical AND (&&) operator 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

Example

function y = dummyFunction() 
for i = 1:6
    if i > 3 && i < 5
        disp('less than 5 and greater than 3')
    else
        disp('greater than 5 and less than 3')
    end
end

Output

greater than 5 and less than 3
greater than 5 and less than 3
greater than 5 and less than 3
less than 5 and greater than 3
greater than 5 and less than 3
greater than 5 and less than 3
Share

You may also like...