octave: Short-circuit Boolean Operators
8.5.2 Short-circuit Boolean Operators
-------------------------------------
Combined with the implicit conversion to scalar values in ‘if’ and
‘while’ conditions, Octave’s element-by-element boolean operators are
often sufficient for performing most logical operations. However, it is
sometimes desirable to stop evaluating a boolean expression as soon as
the overall truth value can be determined. Octave’s “short-circuit”
boolean operators work this way.
‘BOOLEAN1 && BOOLEAN2’
The expression BOOLEAN1 is evaluated and converted to a scalar
using the equivalent of the operation ‘all (BOOLEAN1(:))’. If it
is false, the result of the overall expression is 0. If it is
true, the expression BOOLEAN2 is evaluated and converted to a
scalar using the equivalent of the operation ‘all (BOOLEAN2(:))’.
If it is true, the result of the overall expression is 1.
Otherwise, the result of the overall expression is 0.
*Warning:* there is one exception to the rule of evaluating ‘all
(BOOLEAN1(:))’, which is when ‘boolean1’ is the empty matrix. The
truth value of an empty matrix is always ‘false’ so ‘[] && true’
evaluates to ‘false’ even though ‘all ([])’ is ‘true’.
‘BOOLEAN1 || BOOLEAN2’
The expression BOOLEAN1 is evaluated and converted to a scalar
using the equivalent of the operation ‘all (BOOLEAN1(:))’. If it
is true, the result of the overall expression is 1. If it is
false, the expression BOOLEAN2 is evaluated and converted to a
scalar using the equivalent of the operation ‘all (BOOLEAN2(:))’.
If it is true, the result of the overall expression is 1.
Otherwise, the result of the overall expression is 0.
*Warning:* the truth value of an empty matrix is always ‘false’,
see the previous list item for details.
The fact that both operands may not be evaluated before determining
the overall truth value of the expression can be important. For
example, in the expression
a && b++
the value of the variable B is only incremented if the variable A is
nonzero.
This can be used to write somewhat more concise code. For example,
it is possible write
function f (a, b, c)
if (nargin > 2 && ischar (c))
...
instead of having to use two ‘if’ statements to avoid attempting to
evaluate an argument that doesn’t exist. For example, without the
short-circuit feature, it would be necessary to write
function f (a, b, c)
if (nargin > 2)
if (ischar (c))
...
Writing
function f (a, b, c)
if (nargin > 2 & ischar (c))
...
would result in an error if ‘f’ were called with one or two arguments
because Octave would be forced to try to evaluate both of the operands
for the operator ‘&’.
MATLAB has special behavior that allows the operators ‘&’ and ‘|’ to
short-circuit when used in the truth expression for ‘if’ and ‘while’
statements. Octave also behaves the same way by default, though the use
of the ‘&’ and ‘|’ operators in this way is strongly discouraged.
Instead, you should use the ‘&&’ and ‘||’ operators that always have
short-circuit behavior.
-- : VAL = do_braindead_shortcircuit_evaluation ()
-- : OLD_VAL = do_braindead_shortcircuit_evaluation (NEW_VAL)
-- : do_braindead_shortcircuit_evaluation (NEW_VAL, "local")
Query or set the internal variable that controls whether Octave
will do short-circuit evaluation of ‘|’ and ‘&’ operators inside
the conditions of if or while statements.
This feature is only provided for compatibility with MATLAB and
should not be used unless you are porting old code that relies on
this feature.
To obtain short-circuit behavior for logical expressions in new
programs, you should always use the ‘&&’ and ‘||’ operators.
When called from inside a function with the "local" option, the
variable is changed locally for the function and any subroutines it
calls. The original variable value is restored when exiting the
function.
Finally, the ternary operator (?:) is not supported in Octave. If
short-circuiting is not important, it can be replaced by the ‘ifelse’
function.
-- : merge (MASK, TVAL, FVAL)
-- : ifelse (MASK, TVAL, FVAL)
Merge elements of TRUE_VAL and FALSE_VAL, depending on the value of
MASK.
If MASK is a logical scalar, the other two arguments can be
arbitrary values. Otherwise, MASK must be a logical array, and
TVAL, FVAL should be arrays of matching class, or cell arrays. In
the scalar mask case, TVAL is returned if MASK is true, otherwise
FVAL is returned.
In the array mask case, both TVAL and FVAL must be either scalars
or arrays with dimensions equal to MASK. The result is constructed
as follows:
result(mask) = tval(mask);
result(! mask) = fval(! mask);
MASK can also be arbitrary numeric type, in which case it is first
converted to logical.
See also: logical XREFlogical, diff XREFdiff.