File: Short_002dcircuit-Boolean-Operators.html

package info (click to toggle)
octave3.2 3.2.4-8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 62,936 kB
  • ctags: 37,353
  • sloc: cpp: 219,497; fortran: 116,336; ansic: 10,264; sh: 5,508; makefile: 4,245; lex: 3,573; yacc: 3,062; objc: 2,042; lisp: 1,692; awk: 860; perl: 844
file content (107 lines) | stat: -rw-r--r-- 5,298 bytes parent folder | download
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
<html lang="en">
<head>
<title>Short-circuit Boolean Operators - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<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">
<p>
<a name="Short-circuit-Boolean-Operators"></a>
<a name="Short_002dcircuit-Boolean-Operators"></a>
Previous:&nbsp;<a rel="previous" accesskey="p" href="Element_002dby_002delement-Boolean-Operators.html#Element_002dby_002delement-Boolean-Operators">Element-by-element Boolean Operators</a>,
Up:&nbsp;<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-523"></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> &amp;&amp; </code><var>boolean2</var><dd><a name="index-g_t_0026_0026-524"></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>[] &amp;&amp; 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-525"></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 &amp;&amp; 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 &gt; 2 &amp;&amp; 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 &gt; 2)
         if (ischar (c))
           ...
</pre>
   <p class="noindent">Writing

<pre class="example">     function f (a, b, c)
       if (nargin &gt; 2 &amp; 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 &lsquo;<samp><span class="samp">&amp;</span></samp>&rsquo;.

   </body></html>