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 boolean1 is not a logical value, it is considered true if its value is nonzero, and false if its value is zero. If boolean1 is an array, it is considered true only if it is non-empty and all elements are non-zero. If boolean1 evaluates to false, the result of the overall expression is false. If it is true, the expression boolean2 is evaluated in the same way as boolean1. If it is true, the result of the overall expression is true. Otherwise the result of the overall expression is false.

Warning: the one exception to the equivalence with evaluating all (boolean1(:)) is when boolean1 an the empty array. For MATLAB compatibility, the truth value of an empty array 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 boolean1 is not a logical value, it is considered true if its value is nonzero, and false if its value is zero. If boolean1 is an array, it is considered true only if it is non-empty and all elements are non-zero. If boolean1 evaluates to true, the result of the overall expression is true. If it is false, the expression boolean2 is evaluated in the same way as boolean1. If it is true, the result of the overall expression is true. Otherwise the result of the overall expression is false.

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 behaves the same way for compatibility, however, the use of the ‘&’ and ‘|’ operators in this way is strongly discouraged and a warning will be issued. Instead, you should use the ‘&&’ and ‘||’ operators that always have short-circuit behavior.

Finally, the ternary operator (?:) is not supported in Octave. If short-circuiting is not important, it can be replaced by the ifelse function.

: M = merge (mask, tval, fval)
: M = 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, diff.