<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: A Logical Story</title>
	<atom:link href="http://blogs.mathworks.com/loren/2006/11/01/a-logical-story/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2006/11/01/a-logical-story/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Ed Limbaugh</title>
		<link>http://blogs.mathworks.com/loren/2006/11/01/a-logical-story/#comment-14103</link>
		<dc:creator>Ed Limbaugh</dc:creator>
		<pubDate>Wed, 01 Nov 2006 22:00:17 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=61#comment-14103</guid>
		<description>Ahhhh.  Thanks for clarifying!  I understand more clearly now.</description>
		<content:encoded><![CDATA[<p>Ahhhh.  Thanks for clarifying!  I understand more clearly now.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/11/01/a-logical-story/#comment-14100</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 01 Nov 2006 21:48:22 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=61#comment-14100</guid>
		<description>Ed-

The reason the 2nd one doesn&#039;t need to do scalar expansion is because it is true itself before doing the next evaluation
( xxx &#124; [] ) since 4 is nonzero.  The short-circuiting happens as soon as truth can be determined.  

This was meant to demonstrate that you are MUCH safer using &#124;&#124; and &amp;&amp; and reducing your expressions to scalar values yourself.  Your code will more likely be readable later and do as you expect.  Using the arrays versions, &#124; and &amp;, lead to weirdness, especially when empty arrays might be around.

--Loren</description>
		<content:encoded><![CDATA[<p>Ed-</p>
<p>The reason the 2nd one doesn&#8217;t need to do scalar expansion is because it is true itself before doing the next evaluation<br />
( xxx | [] ) since 4 is nonzero.  The short-circuiting happens as soon as truth can be determined.  </p>
<p>This was meant to demonstrate that you are MUCH safer using || and &#038;&#038; and reducing your expressions to scalar values yourself.  Your code will more likely be readable later and do as you expect.  Using the arrays versions, | and &#038;, lead to weirdness, especially when empty arrays might be around.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ed Limbaugh</title>
		<link>http://blogs.mathworks.com/loren/2006/11/01/a-logical-story/#comment-14099</link>
		<dc:creator>Ed Limbaugh</dc:creator>
		<pubDate>Wed, 01 Nov 2006 21:43:23 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=61#comment-14099</guid>
		<description>If it is a scalar expansion, why does that not take place in the first example, &#039;if 4 &#124; []&#039;?  Are the two cases treated differently with respect to operator priority?  What are the rules for the priority of evaluating the operators in an &#039;if&#039; or &#039;while&#039; statement?   Why would they differ in the two examples?</description>
		<content:encoded><![CDATA[<p>If it is a scalar expansion, why does that not take place in the first example, &#8216;if 4 | []&#8216;?  Are the two cases treated differently with respect to operator priority?  What are the rules for the priority of evaluating the operators in an &#8216;if&#8217; or &#8216;while&#8217; statement?   Why would they differ in the two examples?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/11/01/a-logical-story/#comment-14095</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 01 Nov 2006 20:48:55 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=61#comment-14095</guid>
		<description>All-

I didn&#039;t explain this well, I think.  What happens is that

[] &#124; 4 does a scalar expansion, ends up [] and therefore evaluates to false.  So it&#039;s not the short-circuit that causes the answer, but the scalar expansion.  I will update the article to indicate this.

--Loren</description>
		<content:encoded><![CDATA[<p>All-</p>
<p>I didn&#8217;t explain this well, I think.  What happens is that</p>
<p>[] | 4 does a scalar expansion, ends up [] and therefore evaluates to false.  So it&#8217;s not the short-circuit that causes the answer, but the scalar expansion.  I will update the article to indicate this.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ed Limbaugh</title>
		<link>http://blogs.mathworks.com/loren/2006/11/01/a-logical-story/#comment-14094</link>
		<dc:creator>Ed Limbaugh</dc:creator>
		<pubDate>Wed, 01 Nov 2006 20:35:55 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=61#comment-14094</guid>
		<description>Wouldn&#039;t the defintion of &#039;short-circuit&#039; (with respect to &#039;if&#039; or &#039;while&#039;) be to stop evaluating arguments to an &#039;or&#039; or &#039;and&#039; operator as soon as the final result of the operation can be definitively determined?  Therefore, an &#039;or&#039; operator (&#124;) should continue evaluating arguments until 
    1)  it comes to the first TRUE argument, or
    2)  it has evaluated ALL of the arguments as false.

Similarly, I would expect an &#039;and&#039; operator (&amp;) to continue evaluating arguments until
    1) it comes to the first FALSE  argument, or
    2)  it has evaluated ALL of the arguments as true.

In the first example (&#039;if 4 &#124; []&#039;), the &#039;or&#039; operator short-circuits after evaluating 4 as a true argument.  In the second example (&#039;if [] &#124; 4&#039;), why should &#039;or&#039; stop evaluating?  The first argument is false, as you&#039;ve explained:  &quot;Empty arrays evaluate to false for the purposes of if and while.&quot;  The &#039;or&#039; operator should then continue to evaluate as it has not yet determined whether the remaining arguments are true or false.</description>
		<content:encoded><![CDATA[<p>Wouldn&#8217;t the defintion of &#8216;short-circuit&#8217; (with respect to &#8216;if&#8217; or &#8216;while&#8217;) be to stop evaluating arguments to an &#8216;or&#8217; or &#8216;and&#8217; operator as soon as the final result of the operation can be definitively determined?  Therefore, an &#8216;or&#8217; operator (|) should continue evaluating arguments until<br />
    1)  it comes to the first TRUE argument, or<br />
    2)  it has evaluated ALL of the arguments as false.</p>
<p>Similarly, I would expect an &#8216;and&#8217; operator (&amp;) to continue evaluating arguments until<br />
    1) it comes to the first FALSE  argument, or<br />
    2)  it has evaluated ALL of the arguments as true.</p>
<p>In the first example (&#8216;if 4 | []&#8216;), the &#8216;or&#8217; operator short-circuits after evaluating 4 as a true argument.  In the second example (&#8216;if [] | 4&#8242;), why should &#8216;or&#8217; stop evaluating?  The first argument is false, as you&#8217;ve explained:  &#8220;Empty arrays evaluate to false for the purposes of if and while.&#8221;  The &#8216;or&#8217; operator should then continue to evaluate as it has not yet determined whether the remaining arguments are true or false.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bjorn Gustavsson</title>
		<link>http://blogs.mathworks.com/loren/2006/11/01/a-logical-story/#comment-14091</link>
		<dc:creator>Bjorn Gustavsson</dc:creator>
		<pubDate>Wed, 01 Nov 2006 18:55:47 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=61#comment-14091</guid>
		<description>How can you have &#039;if&#039; shortcut &#039;&#124;&#039; with something that is false?
In my (rapidly made up) opinion this is a severe bug. Lets compare:

if 0 &#124; 4                                          
    disp(&#039;Must be true&#039;)
else
    disp(&#039;Must be false&#039;)
end
Displays:
Must be true

As it should, since &#124; is true since 4 is true. No shortcut in this case.</description>
		<content:encoded><![CDATA[<p>How can you have &#8216;if&#8217; shortcut &#8216;|&#8217; with something that is false?<br />
In my (rapidly made up) opinion this is a severe bug. Lets compare:</p>
<p>if 0 | 4<br />
    disp(&#8216;Must be true&#8217;)<br />
else<br />
    disp(&#8216;Must be false&#8217;)<br />
end<br />
Displays:<br />
Must be true</p>
<p>As it should, since | is true since 4 is true. No shortcut in this case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oliver A. Chapman, PE</title>
		<link>http://blogs.mathworks.com/loren/2006/11/01/a-logical-story/#comment-14086</link>
		<dc:creator>Oliver A. Chapman, PE</dc:creator>
		<pubDate>Wed, 01 Nov 2006 17:04:17 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=61#comment-14086</guid>
		<description>There must be some performance benefit to justify, &quot;The nonscalar &#124; and &amp; operators short-circuit in if and while expressions, but not otherwise.&quot;  Can you give some examples of this benefit?

Before this week&#039;s column, I would have thought that the purpose of the operation was return a true if either of the arguments existed.  Thus, MatLab doesn&#039;t operate as I expect, which is a flaw and should be corrected.

Because of the flaw, I have to add complexity to the code, something like this:


if ~isempty([]) &#124; ~isempty(4)
    disp(&#039;Must be true&#039;)
else
    disp(&#039;Must be false&#039;)
end

if ~isempty(4) &#124; ~isempty([])
    disp(&#039;Must be true&#039;)
else
    disp(&#039;Must be false&#039;)
end


Now I get the expected result.</description>
		<content:encoded><![CDATA[<p>There must be some performance benefit to justify, &#8220;The nonscalar | and &amp; operators short-circuit in if and while expressions, but not otherwise.&#8221;  Can you give some examples of this benefit?</p>
<p>Before this week&#8217;s column, I would have thought that the purpose of the operation was return a true if either of the arguments existed.  Thus, MatLab doesn&#8217;t operate as I expect, which is a flaw and should be corrected.</p>
<p>Because of the flaw, I have to add complexity to the code, something like this:</p>
<p>if ~isempty([]) | ~isempty(4)<br />
    disp(&#8216;Must be true&#8217;)<br />
else<br />
    disp(&#8216;Must be false&#8217;)<br />
end</p>
<p>if ~isempty(4) | ~isempty([])<br />
    disp(&#8216;Must be true&#8217;)<br />
else<br />
    disp(&#8216;Must be false&#8217;)<br />
end</p>
<p>Now I get the expected result.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

