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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Short-circuit Boolean Operators</title>
<meta name="description" content="GNU Octave: Short-circuit Boolean Operators">
<meta name="keywords" content="GNU Octave: Short-circuit Boolean Operators">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Boolean-Expressions.html#Boolean-Expressions" rel="up" title="Boolean Expressions">
<link href="Assignment-Ops.html#Assignment-Ops" rel="next" title="Assignment Ops">
<link href="Element_002dby_002delement-Boolean-Operators.html#Element_002dby_002delement-Boolean-Operators" rel="prev" title="Element-by-element Boolean Operators">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {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}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Short_002dcircuit-Boolean-Operators"></a>
<div class="header">
<p>
Previous: <a href="Element_002dby_002delement-Boolean-Operators.html#Element_002dby_002delement-Boolean-Operators" accesskey="p" rel="prev">Element-by-element Boolean Operators</a>, Up: <a href="Boolean-Expressions.html#Boolean-Expressions" 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#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Short_002dcircuit-Boolean-Operators-1"></a>
<h4 class="subsection">8.5.2 Short-circuit Boolean Operators</h4>
<a name="index-short_002dcircuit-evaluation"></a>
<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><a name="index-_0026_0026"></a>
<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>boolean1</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><a name="index-_007c_007c"></a>
<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>boolean1</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. The Octave parser may be instructed to behave in the
same manner, but its use is strongly discouraged.
</p>
<a name="XREFdo_005fbraindead_005fshortcircuit_005fevaluation"></a><dl>
<dt><a name="index-do_005fbraindead_005fshortcircuit_005fevaluation"></a>Built-in Function: <em><var>val</var> =</em> <strong>do_braindead_shortcircuit_evaluation</strong> <em>()</em></dt>
<dt><a name="index-do_005fbraindead_005fshortcircuit_005fevaluation-1"></a>Built-in Function: <em><var>old_val</var> =</em> <strong>do_braindead_shortcircuit_evaluation</strong> <em>(<var>new_val</var>)</em></dt>
<dt><a name="index-do_005fbraindead_005fshortcircuit_005fevaluation-2"></a>Built-in Function: <em></em> <strong>do_braindead_shortcircuit_evaluation</strong> <em>(<var>new_val</var>, "local")</em></dt>
<dd><p>Query or set the internal variable that controls whether Octave will
do short-circuit evaluation of ‘<samp>|</samp>’ and ‘<samp>&</samp>’ operators inside the
conditions of if or while statements.
</p>
<p>This feature is only provided for compatibility with <small>MATLAB</small> and should
not be used unless you are porting old code that relies on this feature.
</p>
<p>To obtain short-circuit behavior for logical expressions in new programs,
you should always use the ‘<samp>&&</samp>’ and ‘<samp>||</samp>’ operators.
</p>
<p>When called from inside a function with the <code>"local"</code> 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></dd></dl>
<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>
<a name="XREFmerge"></a><dl>
<dt><a name="index-merge"></a>Built-in Function: <em></em> <strong>merge</strong> <em>(<var>mask</var>, <var>tval</var>, <var>fval</var>)</em></dt>
<dt><a name="index-ifelse"></a>Built-in Function: <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>. 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#Element_002dby_002delement-Boolean-Operators" accesskey="p" rel="prev">Element-by-element Boolean Operators</a>, Up: <a href="Boolean-Expressions.html#Boolean-Expressions" 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#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|