[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.5 Boolean Expressions


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.5.1 Element-by-element Boolean Operators

An element-by-element boolean expression is a combination of comparison expressions using the boolean operators "or" (`|'), "and" (`&'), and "not" (`!'), along with parentheses to control nesting. The truth of the boolean expression is computed by combining the truth values of the corresponding elements of the component expressions. A value is considered to be false if it is zero, and true otherwise.

Element-by-element boolean expressions can be used wherever comparison expressions can be used. They can be used in if and while statements. However, if a matrix value used as the condition in an if or while statement is only true if all of its elements are nonzero.

Like comparison operations, each element of an element-by-element boolean expression also has a numeric value (1 if true, 0 if false) that comes into play if the result of the boolean expression is stored in a variable, or used in arithmetic.

Here are descriptions of the three element-by-element boolean operators.

boolean1 & boolean2

Elements of the result are true if both corresponding elements of boolean1 and boolean2 are true.

boolean1 | boolean2

Elements of the result are true if either of the corresponding elements of boolean1 or boolean2 is true.

! boolean
~ boolean

Each element of the result is true if the corresponding element of boolean is false.

For matrix operands, these operators work on an element-by-element basis. For example, the expression

 
[1, 0; 0, 1] & [1, 0; 2, 3]

returns a two by two identity matrix.

For the binary operators, the dimensions of the operands must conform if both are matrices. If one of the operands is a scalar and the other a matrix, the operator is applied to the scalar and each element of the matrix.

For the binary element-by-element boolean operators, both subexpressions boolean1 and boolean2 are evaluated before computing the result. This can make a difference when the expressions have side effects. For example, in the expression

 
a & b++

the value of the variable b is incremented even if the variable a is zero.

This behavior is necessary for the boolean operators to work as described for matrix-valued operands.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

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 (boolean1(:)). 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 (boolean1(:)). 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 && isstr (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 (isstr (c))
      …

Writing

 
function f (a, b, c)
  if (nargin > 2 & isstr (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 `&'.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated on December, 26 2007 using texi2html 1.76.