<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>If statement question</title>
		<link>http://www.allegro.cc/forums/view/602865</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 20 Jan 2010 00:03:01 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello, I was wondering if anyone could tell me: in an if statement, when you have more than one conditionals joined by AND, if the first statement is false does it bother to check the others? Is processing time wasted, and therefor if statements with AND should be separated into two?</p><p>Surely it breaks out of the test, but I&#39;m not entirely sure myself.</p><p>If I am not explaining this well, what I mean is, in the following statement:
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span><span class="k1">bool</span> jumping <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number"> 2</span><span class="k1">bool</span> onGround <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 3</span><span class="k1">if</span><span class="k2">(</span>jumping <span class="k3">&amp;</span><span class="k3">&amp;</span> onGround<span class="k2">)</span> doStuff<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</div></div><p>
Seeing that jumping == false, would onGround even be checked at all? Surely not, but I am not sure. Anybody know the answer?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Falciase)</author>
		<pubDate>Mon, 18 Jan 2010 15:49:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Easy to test. But yes, it will abort early.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonatan Hedborg)</author>
		<pubDate>Mon, 18 Jan 2010 16:08:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So &quot;cascading&quot; your conditional tests can &quot;optimise&quot; your programme? <br />I.e. place boolean tests before one with function calls etc?? Would it be<br />effective to roughly organise the probability of the conditional tests, <br />with the most likely to &quot;fail&quot; first going down to the least likely at the end of the statement?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Yodhe23)</author>
		<pubDate>Mon, 18 Jan 2010 16:21:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, in C and many other language, the logical AND and OR operators are <a href="http://en.wikipedia.org/wiki/Short-circuit_evaluation">short-circuiting</a>.<br />You won&#39;t find any C compiler where the second member of an &quot;&amp;&amp;&quot; is evaluated if the first was resulted in 0. It would be a critical bug.</p><p>Note that this matter mostly when the evaluation involves calling a function (onground()), or if it would do something invalid:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span> <span class="k2">(</span>parent<span class="k3">!</span><span class="k3">=</span>NULL <span class="k3">&amp;</span><span class="k3">&amp;</span> parent-&gt;Type<span class="k3">=</span><span class="k3">=</span>ENEMY<span class="k2">)</span> ...
<span class="k1">if</span> <span class="k2">(</span>divisor<span class="k3">!</span><span class="k3">=</span><span class="n">0</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> height<span class="k3">/</span>divisor<span class="k3">=</span><span class="k3">=</span><span class="n">1</span><span class="k2">)</span> ...
</pre></div></div><p>

Yodhe23 : Yes, on all accounts. But don&#39;t over-do it... It&#39;s worthwhile to avoid expensive 1-N collision tests when (this-&gt;Is_ghost==true), but avoiding one mathematical comparison makes very little difference.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Mon, 18 Jan 2010 16:29:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Audric&#39;s reply is the best.</p><p>If you have this:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>image <span class="k3">=</span> NULL<span class="k2">;</span>

<span class="k1">if</span> <span class="k2">(</span>image <span class="k3">&amp;</span><span class="k3">&amp;</span> image-&gt;w <span class="k3">&lt;</span> <span class="n">10</span><span class="k2">)</span> <span class="k2">{</span> ... <span class="k2">}</span>
</pre></div></div><p>
short-circuiting will stop as soon as it encounters the &quot;<span class="source-code"><span class="k1">if</span> <span class="k2">(</span>image<span class="k2">)</span></span>&quot; portion of that statement.  Otherwise, the &quot;<span class="source-code"><span class="k1">if</span> <span class="k2">(</span>image-&gt;w <span class="k3">&lt;</span> <span class="n">10</span><span class="k2">)</span></span>&quot; would cause a segfault.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OnlineCop)</author>
		<pubDate>Mon, 18 Jan 2010 23:16:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s also a good idea to parenthisize defensively:</p><p>&lt;code&gt;<br />if (image &amp;&amp; (image-&gt;w &lt; 10) ) { ... }<br />&lt;code&gt;
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Mon, 18 Jan 2010 23:23:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span> <span class="k2">(</span><span class="k2">(</span>NULL <span class="k3">!</span><span class="k3">=</span> image<span class="k2">)</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k2">(</span>image-&gt;w <span class="k3">&lt;</span> <span class="k2">(</span><span class="n">9</span> <span class="k3">+</span> <span class="n">2</span> <span class="k3">-</span> <span class="n">1</span><span class="k2">)</span><span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span> ... <span class="k2">}</span>
</pre></div></div><p>
<img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OnlineCop)</author>
		<pubDate>Mon, 18 Jan 2010 23:29:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Defensive is all right, but in this case I would rather recommend to learn the relative priorities of the most common operators.</p><p>Just need to know that there is no &#39;trap&#39; in an expression like:
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// Sprites are 32 pixels wide, only need to be drawn if some part is visible</span>
<span class="k1">if</span> <span class="k2">(</span>x <span class="k3">&gt;</span> <span class="k3">-</span><span class="n">16</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> x <span class="k3">&lt;</span> SCREEN_WIDTH <span class="k3">+</span> <span class="n">16</span><span class="k2">)</span>
<span class="k2">{</span>
   ...
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Tue, 19 Jan 2010 00:12:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks guys for your input. I was pretty much positive this was the case, as that would mean I had been taught wrong, and all other code I had seen had been wrong so I was pretty sure this was the case XD Assurance is good.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Falciase)</author>
		<pubDate>Tue, 19 Jan 2010 01:02:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes; it&#39;s called lazy evalutaion.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Tue, 19 Jan 2010 01:16:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The wikipedia article that I linked mentions a peculiar phenomenon:<br />(a &gt; 0 &amp;&amp; a &lt; 10) would be slower than (a &gt; 0 &amp; a &lt; 10) because of the branch (test+jump) it causes.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Tue, 19 Jan 2010 14:53:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve never seen the use of the single <span class="source-code"><span class="k3">&amp;</span></span> in the context you&#39;ve described.</p><p><span class="source-code"><span class="k2">(</span>a <span class="k3">!</span><span class="k3">=</span> <span class="n">0</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> a <span class="k3">&lt;</span> <span class="n">10</span><span class="k2">)</span></span> would be slightly faster, as the debate in the <a href="http://www.allegro.cc/forums/thread/602869">other thread</a> regarding the <span class="source-code"><span class="k3">&gt;</span></span> and <span class="source-code"><span class="k3">!</span><span class="k3">=</span></span> still rages on, but you&#39;re not going to be MUCH slower.  Plus, with a few nano-seconds in the use of just a handful of evaluations isn&#39;t going to up your performance.  You&#39;ll only see increases when you&#39;re dealing with thousands or more iterations.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OnlineCop)</author>
		<pubDate>Wed, 20 Jan 2010 00:03:01 +0000</pubDate>
	</item>
</rss>
