Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When your input can be clearly separate into distinct cases, I feel it is mostly nicer to explicitly state what those cases are, for example if you are expecting 'n' to be a number between 0 and 100, and you have 3 cases:</p> <pre><code>if (n &gt;= 0 &amp;&amp; n &lt; 30) { case1(); } else if (n &gt;=30 &amp;&amp; n &lt; 70) { case2(); } else if (n &gt;=70 &amp;&amp; n &lt; 100) { case3(); } </code></pre> <p>in some situations the 'else' case is good for error checking</p> <pre><code>} else { error("n should be between 0 and 100"); } </code></pre> <p>if your data is checked for erroneous values earlier, then there may be a case to use else for the final case, to provide a small performance improvement in languages like C:</p> <pre><code>} else { // (n &gt;= 70 &amp;&amp; n &lt; 100) case3(); } </code></pre> <p>but this is only necessary due to some languages inability to express the domain of a function, in languages where the domain can be expressed clearly, the optimiser should add this performance benefit for you, allowing you to be specific in your code, and making it easier to add more cases later</p> <p>... of course this is an art, not a science, and in some cases you can't follow the hard-and-fast rule, I frequently find myself writing code like:</p> <pre><code>if (p == NULL) { doSomething(); } else { doSomethingElse(); } </code></pre> <p>...justified by the fact that is very obvious and implicit from the first if condition what the else is used for.</p>
 

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