<p>I have an if statement with two conditions (separated by an OR operator), one of the conditions covers +70% of situations and takes far less time to process/execute than the second condition, so in the interests of speed I only want the second condition to be processed if the first condition evaluates to false.</p>

<p>if I order the conditions so that the first condition (the quicker one) appears in the if statement first - on the occasions where this condition is met and evaluates true is the second condition even processed?</p>

<pre><code>if ( (condition1) | (condition2) ){
  // do this
}
</code></pre>

<p>or would I need to nest two if statements to only check the second condition if the first evaluates to false?</p>

<pre><code>if (condition1){
  // do this
}else if (condition2){
  // do this
}
</code></pre>

<p>I am working in PHP, however, I assume that this may be language-agnostic.</p>

[{'answer_id': 34941, 'author': 'dagorym', 'author_id': 171, 'author_profile': 'https://Stackoverflow.com/users/171', 'pm_score': 0, 'selected': False, 'text': "<p>Since this is tagged language agnostic I'll chime in.  For Perl at least, the first option is sufficient, I'm not familiar with PHP.  It evaluates left to right and drops out as soon as the condition is met.</p>\n"}, {'answer_id': 34945, 'author': 'NotMe', 'author_id': 2424, 'author_profile': 'https://Stackoverflow.com/users/2424', 'pm_score': 2, 'selected': False, 'text': '<p>Pretty much every language does a short circuit evaluation.  Meaning the second condition is only evaluated if it\'s aboslutely necessary to.  For this to work, most languages use the double pipe, ||, not the single one, |.</p>\n\n<p>See <a href="http://en.wikipedia.org/wiki/Short-circuit_evaluation" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Short-circuit_evaluation</a></p>\n'}, {'answer_id': 34949, 'author': 'levand', 'author_id': 3044, 'author_profile': 'https://Stackoverflow.com/users/3044', 'pm_score': 0, 'selected': False, 'text': '<p>In most languages with decent optimization the former will work just fine.</p>\n'}, {'answer_id': 34954, 'author': 'Mats Fredriksson', 'author_id': 2973, 'author_profile': 'https://Stackoverflow.com/users/2973', 'pm_score': 4, 'selected': True, 'text': '<p>For C, C++, C#, Java and other .NET languages boolean expressions are optimised so that as soon as enough is known nothing else is evaluated.</p>\n\n<p>An old trick for doing obfuscated code was to use this to create if statements, such as:</p>\n\n<pre><code>a || b();\n</code></pre>\n\n<p>if "a" is true, "b()" would never be evaluated, so we can rewrite it into:</p>\n\n<pre><code>if(!a)\n    b();\n</code></pre>\n\n<p>and similarly:</p>\n\n<pre><code>a &amp;&amp; b();\n</code></pre>\n\n<p>would become</p>\n\n<pre><code>if(a)\n    b();\n</code></pre>\n\n<p><strong>Please note</strong> that this is only valid for the || and &amp;&amp; operator. The two operators | and &amp; is bitwise or, and and, respectively, and are therefore not "optimised".</p>\n\n<p>EDIT:\nAs mentioned by others, trying to optimise code using short circuit logic is very rarely well spent time.</p>\n\n<p>First go for clarity, both because it is easier to read and understand. Also, if you try to be too clever a simple reordering of the terms could lead to wildly different behaviour without any apparent reason.</p>\n\n<p>Second, go for optimisation, but only after timing and profiling. Way too many developer  do premature optimisation without profiling. Most of the time it\'s completely useless.</p>\n'}, {'answer_id': 34960, 'author': 'Michael Haren', 'author_id': 29, 'author_profile': 'https://Stackoverflow.com/users/29', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve seen a lot of these types of questions lately--optimization to the nth degree.</p>\n\n<p>I think it makes sense in certain circumstances:</p>\n\n<ol>\n<li>Computing condition 2 is not a constant time operation</li>\n<li>You are asking strictly for educational purposes--you want to know how the language works, not to save 3us.</li>\n</ol>\n\n<p>In other cases, worrying about the "fastest" way to iterate or check a conditional is silly. Instead of writing tests which require millions of trials to see any recordable (but insignificant) difference, focus on clarity.</p>\n\n<p>When someone else (could be you!) picks up this code in a month or a year, what\'s going to be most important is clarity.</p>\n\n<p>In this case, your first example is shorter, clearer and doesn\'t require you to repeat yourself.</p>\n'}, {'answer_id': 34961, 'author': 'Farinha', 'author_id': 2841, 'author_profile': 'https://Stackoverflow.com/users/2841', 'pm_score': 2, 'selected': False, 'text': '<p>According to <a href="http://www.kekh.com/chapter-6/short-circuit-evaluation/" rel="nofollow noreferrer">this article</a> PHP does short circuit evaluation, which means that if the first condition is met the second is not even evaluated.\nIt\'s quite easy to test also (from the article):</p>\n\n<pre><code>&lt;?php\n/* ch06ex07 – shows no output because of short circuit evaluation */\n\nif (true || $intVal = 5) // short circuits after true\n{\n\necho $intVal; // will be empty because the assignment never took place\n}\n\n?&gt;\n</code></pre>\n'}, {'answer_id': 34963, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 2, 'selected': False, 'text': '<p>In C, C++ and Java, the statement:\n<code><pre>\nif (condition1 | condition2) {\n  ...\n}\n</code></pre></p>\n\n<p>will evaluate both conditions every time and only be true if the entire expression is true. </p>\n\n<p>The statement:</p>\n\n<p><pre><code>\nif (condition1 || condition2) {\n  ...\n}\n</pre></code></p>\n\n<p>will evaluate <code>condition2</code> only if <code>condition1</code> is false. The difference is significant if condition2 is a function or another expression with a side-effect. </p>\n\n<p>There is, however, no difference between the <code>||</code> case and the <code>if</code>/<code>else</code> case.</p>\n'}, {'answer_id': 34997, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 0, 'selected': False, 'text': "<p>The <code>|</code> is a bitwise operator in PHP. It does not mean <code>$a OR $b</code>, exactly. You'll want to use the double-pipe. And yes, as mentioned, PHP does short-circuit evaluation. In similar fashion, if the first condition of an <code>&amp;&amp;</code> clause evaluates to false, PHP does not evaluate the rest of the clause, either.</p>\n"}, {'answer_id': 35391, 'author': 'Matt Dillard', 'author_id': 863, 'author_profile': 'https://Stackoverflow.com/users/863', 'pm_score': 1, 'selected': False, 'text': '<p>While using short-circuiting for the purposes of optimization is often overkill, there are certainly other compelling reasons to use it.  One such example (in C++) is the following:</p>\n\n<pre><code>if( pObj != NULL &amp;&amp; *pObj == "username" ) {\n    // Do something...\n}\n</code></pre>\n\n<p>Here, short-circuiting is being relied upon to ensure that <code>pObj</code> has been allocated prior to dereferencing it.  This is far more concise than having nested <code>if</code> statements.</p>\n'}, {'answer_id': 35399, 'author': 'Dillie-O', 'author_id': 71, 'author_profile': 'https://Stackoverflow.com/users/71', 'pm_score': 0, 'selected': False, 'text': '<p>VB.net has two wonderful expression called "OrElse" and "AndAlso"</p>\n\n<p>OrElse will short circuit itself the first time it reaches a True evaluation and execute the code you desire.</p>\n\n<pre><code>If FirstName = "Luke" OrElse FirstName = "Darth" Then\n   Console.Writeline "Greetings Exalted One!"\nEnd If\n</code></pre>\n\n<p>AndAlso will short circuit itself the first time it a False evaluation and not evaluate the code within the block.</p>\n\n<pre><code>If FirstName = "Luke" AndAlso LastName = "Skywalker" Then\n   Console.Writeline "You are the one and only."\nEnd If\n</code></pre>\n\n<p>I find both of these helpful.</p>\n'}, {'answer_id': 77527, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>The short-circuiting is not for optimization. It's main purpose is to avoid calling code that will not work, yet result in a readable test. Example:</p>\n\n<pre><code>if (i &lt; array.size() &amp;&amp; array[i]==foo) ...\n</code></pre>\n\n<p>Note that array[i] may very well get an access violation if i is out of range and crash the program. Thus this program is certainly depending on short-circuiting the evaluation!</p>\n\n<p>I believe this is the reason for writing expressions this way far more often than optimization concerns.</p>\n"}]
['https://Stackoverflow.com/questions/34938', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2083/']