1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Short-circuit Boolean Operators (GNU Octave (version 6.2.0))</title>
<meta name="description" content="Short-circuit Boolean Operators (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Short-circuit Boolean Operators (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Boolean-Expressions.html" rel="up" title="Boolean Expressions">
<link href="Assignment-Ops.html" rel="next" title="Assignment Ops">
<link href="Element_002dby_002delement-Boolean-Operators.html" rel="prev" title="Element-by-element Boolean Operators">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<span id="Short_002dcircuit-Boolean-Operators"></span><div class="header">
<p>
Previous: <a href="Element_002dby_002delement-Boolean-Operators.html" accesskey="p" rel="prev">Element-by-element Boolean Operators</a>, Up: <a href="Boolean-Expressions.html" accesskey="u" rel="up">Boolean Expressions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Short_002dcircuit-Boolean-Operators-1"></span><h4 class="subsection">8.5.2 Short-circuit Boolean Operators</h4>
<span id="index-short_002dcircuit-evaluation"></span>
<p>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
<em>short-circuit</em> boolean operators work this way.
</p>
<dl compact="compact">
<dt><code><var>boolean1</var> && <var>boolean2</var></code></dt>
<dd><span id="index-_0026_0026"></span>
<p>The expression <var>boolean1</var> is evaluated and converted to a scalar
using the equivalent of the operation <code>all (<var>boolean1</var>(:))</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
(<var>boolean2</var>(:))</code>. If it is true, the result of the overall expression
is 1. Otherwise, the result of the overall expression is 0.
</p>
<p><strong>Warning:</strong> there is one exception to the rule of evaluating
<code>all (<var>boolean1</var>(:))</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>.
</p>
</dd>
<dt><code><var>boolean1</var> || <var>boolean2</var></code></dt>
<dd><span id="index-_007c_007c"></span>
<p>The expression <var>boolean1</var> is evaluated and converted to a scalar
using the equivalent of the operation <code>all (<var>boolean1</var>(:))</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
(<var>boolean2</var>(:))</code>. If it is true, the result of the overall expression
is 1. Otherwise, the result of the overall expression is 0.
</p>
<p><strong>Warning:</strong> the truth value of an empty matrix is always <code>false</code>,
see the previous list item for details.
</p></dd>
</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
</p>
<div class="example">
<pre class="example">a && b++
</pre></div>
<p>the value of the variable <var>b</var> is only incremented if the variable
<var>a</var> is nonzero.
</p>
<p>This can be used to write somewhat more concise code. For example, it
is possible write
</p>
<div class="example">
<pre class="example">function f (a, b, c)
if (nargin > 2 && ischar (c))
…
</pre></div>
<p>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
</p>
<div class="example">
<pre class="example">function f (a, b, c)
if (nargin > 2)
if (ischar (c))
…
</pre></div>
<p>Writing
</p>
<div class="example">
<pre class="example">function f (a, b, c)
if (nargin > 2 & ischar (c))
…
</pre></div>
<p>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>&</samp>’.
</p>
<p><small>MATLAB</small> has special behavior that allows the operators ‘<samp>&</samp>’ and
‘<samp>|</samp>’ to short-circuit when used in the truth expression for <code>if</code> and
<code>while</code> statements. Octave behaves the same way for compatibility,
however, the use of the ‘<samp>&</samp>’ and ‘<samp>|</samp>’ operators in this way is
strongly discouraged and a warning will be issued. Instead, you should use
the ‘<samp>&&</samp>’ and ‘<samp>||</samp>’ operators that always have short-circuit behavior.
</p>
<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.
</p>
<span id="XREFmerge"></span><dl>
<dt id="index-merge">: <em></em> <strong>merge</strong> <em>(<var>mask</var>, <var>tval</var>, <var>fval</var>)</em></dt>
<dt id="index-ifelse">: <em></em> <strong>ifelse</strong> <em>(<var>mask</var>, <var>tval</var>, <var>fval</var>)</em></dt>
<dd><p>Merge elements of <var>true_val</var> and <var>false_val</var>, depending on the
value of <var>mask</var>.
</p>
<p>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>
<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:
</p>
<div class="example">
<pre class="example">result(mask) = tval(mask);
result(! mask) = fval(! mask);
</pre></div>
<p><var>mask</var> can also be arbitrary numeric type, in which case it is first
converted to logical.
</p>
<p><strong>See also:</strong> <a href="Logical-Values.html#XREFlogical">logical</a>, <a href="Finding-Elements-and-Checking-Conditions.html#XREFdiff">diff</a>.
</p></dd></dl>
<hr>
<div class="header">
<p>
Previous: <a href="Element_002dby_002delement-Boolean-Operators.html" accesskey="p" rel="prev">Element-by-element Boolean Operators</a>, Up: <a href="Boolean-Expressions.html" accesskey="u" rel="up">Boolean Expressions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|