Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Short-circuiting is where an expression is stopped being evaluated as soon as its outcome is determined. So for instance:</p> <pre><code>if (a == b || c == d || e == f) { // Do something } </code></pre> <p>If <code>a == b</code> is true, then <code>c == d</code> and <code>e == f</code> are <strong>never evaluated at all</strong>, because the expression's outcome has already been determined. if <code>a == b</code> is false, then <code>c == d</code> is evaluated; if <em>it's</em> true, then <code>e == f</code> is never evaluated. This may not seem to make any difference, but consider:</p> <pre><code>if (foo() || bar() || baz()) { // Do something } </code></pre> <p>If <code>foo()</code> returns true, then <code>bar</code> and <code>baz</code> are <strong>never called</strong>, because the expression's outcome has already been determined. So if <code>bar</code> or <code>baz</code> has some other effect than just returning something (a <em>side effect</em>), those effects never occur.</p> <p>One great example of short-circuiting relates to object references:</p> <pre><code>if (a != null &amp;&amp; a.getFoo() != 42) { // Do something } </code></pre> <p><code>a.getFoo()</code> would normally throw a <code>NullPointerException</code> if <code>a</code> were <code>null</code>, but because the expression short-circuits, if <code>a != null</code> is <code>false</code>, the <code>a.getFoo()</code> part never happens, so we don't get an exception.</p> <p>Note that not all expressions are short-circuited. The <code>||</code> and <code>&amp;&amp;</code> operators are short-circuited, but <code>|</code> and <code>&amp;</code> are not, nor are <code>*</code> or <code>/</code>; in fact most operators are not.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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