Is the logical & operator in MATLAB a short circuit operator ?

3 views (last 30 days)
Is the logical & operator in MATLAB a short circuit operator such that if the first argument is false, the second argument is not evaluated? Are the arguments evaluated in the order they appear ?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
As of MATLAB 5.3 (R11), the logical & operator does not evaluate the second argument if the first is false; ie the operation is short circuited. The arguments are also evaluated in the order they are given. This is also the case for the logical | (OR) operator.
Please note however, that this is only true in IF and WHILE statements. For example, in the following code:
y = 0 & launch(1);
launch will be called. But if you do the following instead:
if (0 & launch(1)), end
launch will not be evaluated.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!