Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Taking advantage of short-circuiting in boolean expressions:</p> <pre><code>int max(int a, int b, int c) { int m = a; (m &lt; b) &amp;&amp; (m = b); //these are not conditional statements. (m &lt; c) &amp;&amp; (m = c); //these are just boolean expressions. return m; } </code></pre> <p><strong>Explanation:</strong> </p> <p>In boolean <code>AND</code> operation such as <code>x &amp;&amp; y</code>, y is evaluated <strong>if and only if</strong> <code>x</code> is true. If <code>x</code> is false, then <code>y</code> is not evaluated, because the whole expression would be false which can be deduced without even evaluating <code>y</code>. This is called short-circuiting when the value of a boolean expression can be deduced without evaluating all operands in it.</p> <p>Apply this principle to the above code. Initially <code>m</code> is <code>a</code>. Now if <code>(m &lt; b)</code> is true, then that means, <code>b</code> is greater than <code>m</code> (which is actually <code>a</code>), so the second subexpression <code>(m = b)</code> is evaluated and <code>m</code> is set to <code>b</code>. If however <code>(m &lt; b)</code> is false, then second subexpression will not be evaluated and <code>m</code> will remain <code>a</code> (which is greater than <code>b</code>). In a similar way, second expression is evaluated (on the next line).</p> <p>In short, you can read the expression <code>(m &lt; x) &amp;&amp; (m = x)</code> as follows : set <code>m</code> to <code>x</code> <strong>if and only if</strong> <code>m</code> is less than <code>x</code> i.e <code>(m &lt; x)</code> is true. Hope this helps you understanding the code.</p> <p><strong>Test code:</strong> </p> <pre><code>int main() { printf("%d\n", max(1,2,3)); printf("%d\n", max(2,3,1)); printf("%d\n", max(3,1,2)); return 0; } </code></pre> <p><strong>Output:</strong></p> <pre><code>3 3 3 </code></pre> <p><strong>Online Demo:</strong> <a href="http://www.ideone.com/8045P" rel="noreferrer">http://www.ideone.com/8045P</a></p> <p>Note the implementation of <code>max</code> gives warnings because evaluated expressions are not used:</p> <blockquote> <p>prog.c:6: warning: value computed is not used<br> prog.c:7: warning: value computed is not used</p> </blockquote> <p>To avoid these (harmless) warnings, you can implement <code>max</code> as:</p> <pre><code>int max(int a, int b, int c) { int m = a; (void)((m &lt; b) &amp;&amp; (m = b)); //these are not conditional statements. (void)((m &lt; c) &amp;&amp; (m = c)); //these are just boolean expressions. return m; } </code></pre> <p>The trick is that now we're <a href="https://stackoverflow.com/questions/689677/casting-unused-return-values-to-void/7160837#7160837">casting the boolean expressions to <code>void</code>, which causes suppression of the warnings</a>: </p> <ul> <li><a href="http://www.ideone.com/PZ1sP" rel="noreferrer">http://www.ideone.com/PZ1sP</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload