while - while loop to repeat when condition
is true - MATLAB (original) (raw)
An expression can include relational operators (such as <
or ==
) and logical operators (such as &&
, ||
, or ~
). Use the logical operators and
and or
to create compound expressions. MATLABĀ® evaluates compound expressions from left to right, adhering to operator precedence rules.
Within the conditional expression of a while...end
block, logical operators &
and |
behave as short-circuit operators. This behavior is the same as &&
and ||
, respectively. Since &&
and ||
consistently short-circuit in conditional expressions and statements, it is good practice to use &&
and ||
instead of &
and |
within the expression. For example,
x = 42; while exist('myfunction.m','file') && (myfunction(x) >= pi) disp('Expressions are true') break end
The first part of the expression evaluates to false. Therefore, MATLAB does not need to evaluate the second part of the expression, which would result in an undefined function error.