Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>||</code> and <code>&amp;&amp;</code> uses <a href="http://en.wikipedia.org/wiki/Short-circuit_evaluation" rel="nofollow noreferrer">short circuit evaluation</a></p> <p>From <a href="http://en.wikipedia.org/wiki/Short-circuit_evaluation" rel="nofollow noreferrer">same article</a></p> <blockquote> <p>Short-circuit evaluation, minimal evaluation, or McCarthy evaluation denotes the semantics of some Boolean operators in some programming languages in which <strong>the second argument is only executed or evaluated if the first argument does not suffice</strong> to determine the value of the expression</p> </blockquote> <p>Consider you have an object and you want to check one of its property for some value if you do:</p> <pre><code>if(obj != null &amp; obj.ID == 1) </code></pre> <p>If your object <code>obj</code> is null you will get a null reference exception, since you used single <code>&amp;</code>, the first condition evaluate to <code>false</code>, but it will still continue to the next condition and raising null reference exception. </p> <p>If you used <code>&amp;&amp;</code> then the second condition will never get evaluated, thus no exception. </p> <pre><code>if(obj != null &amp;&amp; obj.ID == 1) </code></pre> <p>With your code you will not see any difference, but using bitwise <code>|</code> or <code>&amp;</code> with multiple conditions would result in no short circuiting at all. </p> <p>More Explanation. </p> <hr> <p>consider that you following code:</p> <pre><code>Boolean conditionA = 2 &gt; 1; //some condition which returns true if(conditionA | obj.SomeTimeConsumingMethod()){ } </code></pre> <p>Now in above code snippet, suppose you have an object with a method <code>SomeTimeConsumingMethod</code> which takes a lot of time in processing and returns <code>true</code> or <code>false</code>. If you use single <code>|</code> it would evaluate both the conditions, since first <code>conditionA</code> is <code>true</code> it will process <code>obj.SomeTimeConsumingMethod</code> as well. End result will be <code>true</code> for the whole <code>if</code> statement since <code>|</code> <em>(OR)</em> is used. </p> <p>If your condition is using double <code>||</code> <em>(OR Logical operator)</em> </p> <pre><code>if(conditionA || obj.SomeTimeConsumingMethod()) </code></pre> <p>Then the second condition <code>obj.SomeTimeConsumingMethod()</code> will not be evaluated. This is short circuiting and that just saved you from executing some time consuming method. Still the end result is <code>true</code> regardless of what was returned from <code>obj.SomeTimeConsumingMethod()</code>. </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. 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.
 

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