<html lang="en"> <head> <title>Short-circuit Boolean Operators - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Boolean-Expressions.html#Boolean-Expressions" title="Boolean Expressions"> <link rel="prev" href="Element_002dby_002delement-Boolean-Operators.html#Element_002dby_002delement-Boolean-Operators" title="Element-by-element Boolean Operators"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Short-circuit-Boolean-Operators"></a> <a name="Short_002dcircuit-Boolean-Operators"></a> <p> Previous: <a rel="previous" accesskey="p" href="Element_002dby_002delement-Boolean-Operators.html#Element_002dby_002delement-Boolean-Operators">Element-by-element Boolean Operators</a>, Up: <a rel="up" accesskey="u" href="Boolean-Expressions.html#Boolean-Expressions">Boolean Expressions</a> <hr> </div> <h4 class="subsection">8.5.2 Short-circuit Boolean Operators</h4> <p><a name="index-short_002dcircuit-evaluation-652"></a> Combined with the implicit conversion to scalar values in <code>if</code> and <code>while</code> 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 <dfn>short-circuit</dfn> boolean operators work this way. <dl> <dt><var>boolean1</var><code> && </code><var>boolean2</var><dd><a name="index-g_t_0026_0026-653"></a>The expression <var>boolean1</var> is evaluated and converted to a scalar using the equivalent of the operation <code>all (</code><var>boolean1</var><code>(:))</code>. If it is false, the result of the overall expression is 0. If it is true, the expression <var>boolean2</var> is evaluated and converted to a scalar using the equivalent of the operation <code>all (</code><var>boolean1</var><code>(:))</code>. If it is true, the result of the overall expression is 1. Otherwise, the result of the overall expression is 0. <p><strong>Warning:</strong> there is one exception to the rule of evaluating <code>all (</code><var>boolean1</var><code>(:))</code>, which is when <code>boolean1</code> is the empty matrix. The truth value of an empty matrix is always <code>false</code> so <code>[] && true</code> evaluates to <code>false</code> even though <code>all ([])</code> is <code>true</code>. <br><dt><var>boolean1</var><code> || </code><var>boolean2</var><dd><a name="index-g_t_007c_007c-654"></a>The expression <var>boolean1</var> is evaluated and converted to a scalar using the equivalent of the operation <code>all (</code><var>boolean1</var><code>(:))</code>. If it is true, the result of the overall expression is 1. If it is false, the expression <var>boolean2</var> is evaluated and converted to a scalar using the equivalent of the operation <code>all (</code><var>boolean1</var><code>(:))</code>. If it is true, the result of the overall expression is 1. Otherwise, the result of the overall expression is 0. <p><strong>Warning:</strong> the truth value of an empty matrix is always <code>false</code>, see the previous list item for details. </dl> <p>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 <pre class="example"> a && b++ </pre> <p class="noindent">the value of the variable <var>b</var> is only incremented if the variable <var>a</var> is nonzero. <p>This can be used to write somewhat more concise code. For example, it is possible write <pre class="example"> function f (a, b, c) if (nargin > 2 && ischar (c)) ... </pre> <p class="noindent">instead of having to use two <code>if</code> 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 <pre class="example"> function f (a, b, c) if (nargin > 2) if (ischar (c)) ... </pre> <p class="noindent">Writing <pre class="example"> function f (a, b, c) if (nargin > 2 & ischar (c)) ... </pre> <p class="noindent">would result in an error if <code>f</code> were called with one or two arguments because Octave would be forced to try to evaluate both of the operands for the operator ‘<samp><span class="samp">&</span></samp>’. <p><span class="sc">matlab</span> has special behavior that allows the operators ‘<samp><span class="samp">&</span></samp>’ and ‘<samp><span class="samp">|</span></samp>’ to short-circuit when used in the truth expression for <code>if</code> and <code>while</code> statements. The Octave parser may be instructed to behave in the same manner, but its use is strongly discouraged. <!-- do_braindead_shortcircuit_evaluation src/pt-binop.cc --> <p><a name="doc_002ddo_005fbraindead_005fshortcircuit_005fevaluation"></a> <div class="defun"> — Built-in Function: <var>val</var> = <b>do_braindead_shortcircuit_evaluation</b> ()<var><a name="index-do_005fbraindead_005fshortcircuit_005fevaluation-655"></a></var><br> — Built-in Function: <var>old_val</var> = <b>do_braindead_shortcircuit_evaluation</b> (<var>new_val</var>)<var><a name="index-do_005fbraindead_005fshortcircuit_005fevaluation-656"></a></var><br> — Built-in Function: <b>do_braindead_shortcircuit_evaluation</b> (<var>new_val, "local"</var>)<var><a name="index-do_005fbraindead_005fshortcircuit_005fevaluation-657"></a></var><br> <blockquote><p>Query or set the internal variable that controls whether Octave will do short-circuit evaluation of ‘<samp><span class="samp">|</span></samp>’ and ‘<samp><span class="samp">&</span></samp>’ operators inside the conditions of if or while statements. <p>This feature is only provided for compatibility with <span class="sc">matlab</span> and should not be used unless you are porting old code that relies on this feature. <p>To obtain short-circuit behavior for logical expressions in new programs, you should always use the ‘<samp><span class="samp">&&</span></samp>’ and ‘<samp><span class="samp">||</span></samp>’ operators. <p>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. </p></blockquote></div> <p>Finally, the ternary operator (?:) is not supported in Octave. If short-circuiting is not important, it can be replaced by the <code>ifelse</code> function. <!-- merge src/data.cc --> <p><a name="doc_002dmerge"></a> <div class="defun"> — Built-in Function: <b>merge</b> (<var>mask, tval, fval</var>)<var><a name="index-merge-658"></a></var><br> — Built-in Function: <b>ifelse</b> (<var>mask, tval, fval</var>)<var><a name="index-ifelse-659"></a></var><br> <blockquote><p>Merge elements of <var>true_val</var> and <var>false_val</var>, depending on the value of <var>mask</var>. If <var>mask</var> is a logical scalar, the other two arguments can be arbitrary values. Otherwise, <var>mask</var> must be a logical array, and <var>tval</var>, <var>fval</var> should be arrays of matching class, or cell arrays. In the scalar mask case, <var>tval</var> is returned if <var>mask</var> is true, otherwise <var>fval</var> is returned. <p>In the array mask case, both <var>tval</var> and <var>fval</var> must be either scalars or arrays with dimensions equal to <var>mask</var>. The result is constructed as follows: <pre class="example"> result(mask) = tval(mask); result(! mask) = fval(! mask); </pre> <p><var>mask</var> can also be arbitrary numeric type, in which case it is first converted to logical. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dlogical.html#doc_002dlogical">logical</a>. </p></blockquote></div> </body></html>