<?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: Interesting Matlab puzzle	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/interesting-matlab-puzzle/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/interesting-matlab-puzzle?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=interesting-matlab-puzzle</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Mon, 08 Apr 2019 02:00:09 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>
		By: Brad Stiritz		</title>
		<link>https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-473457</link>

		<dc:creator><![CDATA[Brad Stiritz]]></dc:creator>
		<pubDate>Mon, 08 Apr 2019 02:00:09 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8614#comment-473457</guid>

					<description><![CDATA[Hi Yair, I would expect the conditional expression to fail during parsing, and therefore lead to &quot;Doo!&quot; output per catch block. The proper &quot;functional&quot; syntax for logical OR is as follows:
&lt;pre lang=&quot;matlab&quot;&gt;
&gt;&gt; or(false,true)
ans =
  logical
   1
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>Hi Yair, I would expect the conditional expression to fail during parsing, and therefore lead to &#8220;Doo!&#8221; output per catch block. The proper &#8220;functional&#8221; syntax for logical OR is as follows:</p>
<pre lang="matlab">
>> or(false,true)
ans =
  logical
   1
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Marshall		</title>
		<link>https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-472724</link>

		<dc:creator><![CDATA[Marshall]]></dc:creator>
		<pubDate>Thu, 04 Apr 2019 17:39:28 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8614#comment-472724</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-472721&quot;&gt;Marshall&lt;/a&gt;.

(I meant variant #5)]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-472721">Marshall</a>.</p>
<p>(I meant variant #5)</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Marshall		</title>
		<link>https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-472722</link>

		<dc:creator><![CDATA[Marshall]]></dc:creator>
		<pubDate>Thu, 04 Apr 2019 17:38:40 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8614#comment-472722</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-472721&quot;&gt;Marshall&lt;/a&gt;.

Ha, it looks like variant #4 got me. Quite clever. When pasted in Matlab it&#039;s a bit more obvious because the &quot;10&#060; 9.9&#034; is coded purple, indicating character arrays.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-472721">Marshall</a>.</p>
<p>Ha, it looks like variant #4 got me. Quite clever. When pasted in Matlab it&#8217;s a bit more obvious because the &#8220;10&lt; 9.9&quot; is coded purple, indicating character arrays.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Marshall		</title>
		<link>https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-472721</link>

		<dc:creator><![CDATA[Marshall]]></dc:creator>
		<pubDate>Thu, 04 Apr 2019 17:35:38 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8614#comment-472721</guid>

					<description><![CDATA[It looks like this is just syntax muddling. Rewritten with better formatting, we get this:
&lt;pre lang=&quot;matlab&quot;&gt;
function test
    try
        if(false)
            or(true);
            disp(&#039;Yaba&#039;);
        else
            disp(&#039;Daba&#039;);
        end
    catch
        disp(&#039;Doo!&#039;);
    end
end
&lt;/pre&gt;

We never actually reach the line &quot;or(true)&quot;, which would throw an error, causing the catch block to print &#039;Doo!&#039;, so instead we simply print &#039;Daba&#039;. The parentheses are required in order to have the (false) alone apply to the if statement, as opposed to what comes after.

Regarding the other questions:

if (false) or (true)     % variant #1
if (true)  or (false)    % variant #2 --- my guess is this will print &#039;Doo!&#039;, since the or(false) will throw an error
if (true)  or (10&#060; 9.9)  % variant #3 --- will also print &#039;Doo!&#039; for the same reason: &quot;or(10&#060;9.9)&quot; is invalid
if  true   or  10&#060; 9.9   % variant #4 --- will also print &#039;Doo!&#039;, but this time it&#039;s because &quot;true or 10&#060; 9.9&quot; or: &quot;10&#062;9.9 or 10&#060;9.9&quot; is invalid -- the or function requires 2 arguments]]></description>
			<content:encoded><![CDATA[<p>It looks like this is just syntax muddling. Rewritten with better formatting, we get this:</p>
<pre lang="matlab">
function test
    try
        if(false)
            or(true);
            disp('Yaba');
        else
            disp('Daba');
        end
    catch
        disp('Doo!');
    end
end
</pre>
<p>We never actually reach the line &#8220;or(true)&#8221;, which would throw an error, causing the catch block to print &#8216;Doo!&#8217;, so instead we simply print &#8216;Daba&#8217;. The parentheses are required in order to have the (false) alone apply to the if statement, as opposed to what comes after.</p>
<p>Regarding the other questions:</p>
<p>if (false) or (true)     % variant #1<br />
if (true)  or (false)    % variant #2 &#8212; my guess is this will print &#8216;Doo!&#8217;, since the or(false) will throw an error<br />
if (true)  or (10&lt; 9.9)  % variant #3 &#8212; will also print &#8216;Doo!&#8217; for the same reason: &#8220;or(10&lt;9.9)&#8221; is invalid<br />
if  true   or  10&lt; 9.9   % variant #4 &#8212; will also print &#8216;Doo!&#8217;, but this time it&#8217;s because &#8220;true or 10&lt; 9.9&#8221; or: &#8220;10&gt;9.9 or 10&lt;9.9&#8221; is invalid &#8212; the or function requires 2 arguments</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Michelle Hirsch		</title>
		<link>https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-472001</link>

		<dc:creator><![CDATA[Michelle Hirsch]]></dc:creator>
		<pubDate>Mon, 01 Apr 2019 15:57:49 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8614#comment-472001</guid>

					<description><![CDATA[Is it cheating if I not only work at MathWorks, but asked the development lead for the MATLAB Front End and Code Analyzer for an answer? Andrew emailed me the same puzzle over the weekend, which I forwarded on to the dev lead. Here&#039;s the scoop:

For backwards compatibility, space is a valid separator between the expression in the if-condition and the first statement in the body. We don&#039;t want to encourage users to write more such code because we realize this can be very confusing, but we also don&#039;t want to break existing code which is out there and works correctly. 
 
Therefore, the Code Analyzer tries to provide an indication of the current behavior and direct users away from it. For this specific code example, it provides these two messages on the &quot;or&quot; in the MATLAB Editor:
 
&lt;blockquote&gt;&lt;code&gt;This statement (and possibly following ones) cannot be reached.
Consider using newline, semicolon, or comma before this statement for readability.&lt;/code&gt;&lt;/blockquote&gt;]]></description>
			<content:encoded><![CDATA[<p>Is it cheating if I not only work at MathWorks, but asked the development lead for the MATLAB Front End and Code Analyzer for an answer? Andrew emailed me the same puzzle over the weekend, which I forwarded on to the dev lead. Here&#8217;s the scoop:</p>
<p>For backwards compatibility, space is a valid separator between the expression in the if-condition and the first statement in the body. We don&#8217;t want to encourage users to write more such code because we realize this can be very confusing, but we also don&#8217;t want to break existing code which is out there and works correctly. </p>
<p>Therefore, the Code Analyzer tries to provide an indication of the current behavior and direct users away from it. For this specific code example, it provides these two messages on the &#8220;or&#8221; in the MATLAB Editor:</p>
<blockquote><p><code>This statement (and possibly following ones) cannot be reached.<br />
Consider using newline, semicolon, or comma before this statement for readability.</code></p></blockquote>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Will		</title>
		<link>https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-471925</link>

		<dc:creator><![CDATA[Will]]></dc:creator>
		<pubDate>Mon, 01 Apr 2019 09:57:06 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8614#comment-471925</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-471920&quot;&gt;Will&lt;/a&gt;.

Oh dear, seems I was wrong on (at least) two counts above...

First, the first set of brackets seem to do nothing. I was sure you normally needed a comma after the condition expression, but apparently not?!

Second, the command in variant #4 still doesn&#039;t qualify as part of the &lt;i&gt;if&lt;/i&gt; condition. So after having satisfied the &lt;i&gt;if true&lt;/i&gt; condition, first the result of the unsuppressed command &lt;i&gt;or 10&#060; 9.9&lt;/i&gt; is displayed in the command window:

&lt;pre lang=&quot;matlab&quot;&gt;ans =
  1×3 logical array
   1   1   1&lt;/pre&gt;
&lt;i&gt;then&lt;/i&gt; &#039;Yaba&#039; is displayed!]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-471920">Will</a>.</p>
<p>Oh dear, seems I was wrong on (at least) two counts above&#8230;</p>
<p>First, the first set of brackets seem to do nothing. I was sure you normally needed a comma after the condition expression, but apparently not?!</p>
<p>Second, the command in variant #4 still doesn&#8217;t qualify as part of the <i>if</i> condition. So after having satisfied the <i>if true</i> condition, first the result of the unsuppressed command <i>or 10&lt; 9.9</i> is displayed in the command window:</p>
<pre lang="matlab">ans =
  1×3 logical array
   1   1   1</pre>
<p><i>then</i> &#8216;Yaba&#8217; is displayed!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Will		</title>
		<link>https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-471920</link>

		<dc:creator><![CDATA[Will]]></dc:creator>
		<pubDate>Mon, 01 Apr 2019 09:30:43 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8614#comment-471920</guid>

					<description><![CDATA[Very intriguing puzzle! I never knew that parentheses around the expression immediately following &lt;i&gt;if&lt;/i&gt; allowed you to continue with the first line of the statement group without a comma or semicolon. Is this syntactic logic really not documented? I certainly couldn&#039;t find any.

That behaviour is of course the reason the main puzzle results in &lt;i&gt;Daba&lt;/i&gt;, after seeing that the full condition of the &lt;i&gt;if&lt;/i&gt; statement is just &lt;i&gt;false&lt;/i&gt;. I don&#039;t think that an &lt;i&gt;if&lt;/i&gt; statement skipping the block it would execute if true and proceeding to the &lt;i&gt;else&lt;/i&gt; block can be described as short-circuit evaluation, but maybe my understanding of execution engine theory falls short here.

The variants are fun, too.

In variant #2 we pass the &lt;i&gt;if&lt;/i&gt; condition and get to the function call to &lt;i&gt;or&lt;/i&gt; (disguised as a binary operator to fool users of more verbose languages than MATLAB) with only one argument - throwing an exception that results in a &lt;i&gt;Doo!&lt;/i&gt;

In variant #3 nothing changes, because of course &lt;i&gt;10&#060; 9.9&lt;/i&gt; just evaluates to a scalar &lt;i&gt;false&lt;/i&gt; which still isn&#039;t an adequate input to the &lt;i&gt;or&lt;/i&gt; function. But of course this is just a set up for

variant #4, which cunningly uses command syntax to pass two character vector arguments to &lt;i&gt;or&lt;/i&gt;, each carefully engineered to be three characters long so that we evaluate the equivalent of &lt;i&gt;&#039;10&#060;&#039; &#124; &#039;9.9&#039;&lt;/i&gt; which is a valid vector boolean operation. Since none of the characters involved is the null character the result is `[true true true]` so we finally get a `Yaba`.

Happy April Fools&#039;!]]></description>
			<content:encoded><![CDATA[<p>Very intriguing puzzle! I never knew that parentheses around the expression immediately following <i>if</i> allowed you to continue with the first line of the statement group without a comma or semicolon. Is this syntactic logic really not documented? I certainly couldn&#8217;t find any.</p>
<p>That behaviour is of course the reason the main puzzle results in <i>Daba</i>, after seeing that the full condition of the <i>if</i> statement is just <i>false</i>. I don&#8217;t think that an <i>if</i> statement skipping the block it would execute if true and proceeding to the <i>else</i> block can be described as short-circuit evaluation, but maybe my understanding of execution engine theory falls short here.</p>
<p>The variants are fun, too.</p>
<p>In variant #2 we pass the <i>if</i> condition and get to the function call to <i>or</i> (disguised as a binary operator to fool users of more verbose languages than MATLAB) with only one argument &#8211; throwing an exception that results in a <i>Doo!</i></p>
<p>In variant #3 nothing changes, because of course <i>10&lt; 9.9</i> just evaluates to a scalar <i>false</i> which still isn&#8217;t an adequate input to the <i>or</i> function. But of course this is just a set up for</p>
<p>variant #4, which cunningly uses command syntax to pass two character vector arguments to <i>or</i>, each carefully engineered to be three characters long so that we evaluate the equivalent of <i>&#8217;10&lt;&#8216; | &#8216;9.9&#8217;</i> which is a valid vector boolean operation. Since none of the characters involved is the null character the result is `[true true true]` so we finally get a `Yaba`.</p>
<p>Happy April Fools&#8217;!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Codrut Dan		</title>
		<link>https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-471884</link>

		<dc:creator><![CDATA[Codrut Dan]]></dc:creator>
		<pubDate>Mon, 01 Apr 2019 07:04:03 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8614#comment-471884</guid>

					<description><![CDATA[Hi everyone,

new here :) 
 
nice puzzle. I have never used &#039;or&#039; like that, only used function or(A,B), or the &#124;&#124; operator. I would have expected error there...so that was my answer :)
Now I&#039;ll need to review my knowledge on syntax...]]></description>
			<content:encoded><![CDATA[<p>Hi everyone,</p>
<p>new here 🙂 </p>
<p>nice puzzle. I have never used &#8216;or&#8217; like that, only used function or(A,B), or the || operator. I would have expected error there&#8230;so that was my answer 🙂<br />
Now I&#8217;ll need to review my knowledge on syntax&#8230;</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Rik Wisselink		</title>
		<link>https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-471713</link>

		<dc:creator><![CDATA[Rik Wisselink]]></dc:creator>
		<pubDate>Sun, 31 Mar 2019 16:54:18 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8614#comment-471713</guid>

					<description><![CDATA[Prior to running it, I would have expected the if to error. Then I pasted it into the editor and the lint told me to put a comma after the false. That caused me to believe it saw the false as the complete conditional, which would cause the else to be executed (which seems to be the case when running it).

I suspect the reason for this stems from the support for a single line if. I suspect Matlab parses everything from the if to the end of the first function call (or of course &#124;&#124; or &#038;&#038;) as the conditional.

&lt;pre lang=&quot;matlab&quot;&gt;
%triggers the else branch:
if (true &amp;&amp; false) or (true)
%  ^. . . . . . .^
%this is parsed as the conditional (space delimited command)
if (true &amp;&amp; false) or (true)
%                  ^. . . .^ 
%This is considered the code to be executed on if, because there is no
%logical operator there, and the preceding part is a complete command.
%Removing the parentheses there will not make a difference, as the space is
%still demarcating the logical statement from the consequence code.
&lt;/pre&gt;
Replacing the true and if statements by anything that returns those statements does not change the parsing. Removing the parentheses will only change the or() call to be in the char mode, which would then still be missing its second input. Curiously, you can&#039;t enter a false in char mode. If you try with eval to have a char(0) to force a false, an error is returned instead (not enough inputs), as eval seems to crop from char(0) onwards.]]></description>
			<content:encoded><![CDATA[<p>Prior to running it, I would have expected the if to error. Then I pasted it into the editor and the lint told me to put a comma after the false. That caused me to believe it saw the false as the complete conditional, which would cause the else to be executed (which seems to be the case when running it).</p>
<p>I suspect the reason for this stems from the support for a single line if. I suspect Matlab parses everything from the if to the end of the first function call (or of course || or &amp;&amp;) as the conditional.</p>
<pre lang="matlab">
%triggers the else branch:
if (true &#038;& false) or (true)
%  ^. . . . . . .^
%this is parsed as the conditional (space delimited command)
if (true &#038;& false) or (true)
%                  ^. . . .^ 
%This is considered the code to be executed on if, because there is no
%logical operator there, and the preceding part is a complete command.
%Removing the parentheses there will not make a difference, as the space is
%still demarcating the logical statement from the consequence code.
</pre>
<p>Replacing the true and if statements by anything that returns those statements does not change the parsing. Removing the parentheses will only change the or() call to be in the char mode, which would then still be missing its second input. Curiously, you can&#8217;t enter a false in char mode. If you try with eval to have a char(0) to force a false, an error is returned instead (not enough inputs), as eval seems to crop from char(0) onwards.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Roger Watt		</title>
		<link>https://undocumentedmatlab.com/articles/interesting-matlab-puzzle#comment-471709</link>

		<dc:creator><![CDATA[Roger Watt]]></dc:creator>
		<pubDate>Sun, 31 Mar 2019 16:36:44 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8614#comment-471709</guid>

					<description><![CDATA[this one works as expected:
&lt;pre lang=&quot;matlab&quot;&gt;
if (false) &#124;&#124; (true)
etc
&lt;/pre&gt;
the parsing is:
&lt;pre lang=&quot;matlab&quot;&gt;
if false,
 or(true)
 disp(1)
else
 disp(2)
end
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>this one works as expected:</p>
<pre lang="matlab">
if (false) || (true)
etc
</pre>
<p>the parsing is:</p>
<pre lang="matlab">
if false,
 or(true)
 disp(1)
else
 disp(2)
end
</pre>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
