Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I just wonder if having two conditions in an IF statement executes faster than having it separated into two like these:

Single Statement, two conditions

if ( (conditionA == true) && (conditionB == true) )
  //Do something;

Two statements

if ( conditionA == true )
  if ( conditionB == true )
    //Do something

Or are they just the same when compiled?

Both of them have the same code, only the if statement part is different. There won't be any other code under conditionA in the second example (two statements), only afer conditionB.

Does this have an effect on PHP and JavaScript?

share|improve this question
5  
The only answer to your question is "it doesn't matter". –  MattDavey May 23 '12 at 9:35
2  
Downvote because this question is impossible to answer. –  MattDavey May 23 '12 at 9:37
1  
It is not. Maybe if you rephrase the question to say "does this C# code compile to the same CIL" it makes sense - but then it's a question for StackOverflow. –  MattDavey May 23 '12 at 9:40
3  
Benchmark it and see for your self. –  Songo May 23 '12 at 9:40
2  
@MegaNairda Although anyone can revise and improve your question, no one (but you) is required to do it. Down votes are appropriate for questions that are as vague as yours (at least the first version of it), please consider concentrating your efforts on taking the feedback given and improving your question instead of engaging in unproductive comment discussions. –  Yannis Rizos May 23 '12 at 9:55

6 Answers 6

up vote 6 down vote accepted

There is no single answer to this question.

First of all, you have not stated a specific programming language, in which case the question can go to programming languages in general.

Some programming languages will short circuit the evaluation, i.e. not evaluate conditionB if conditionA is false. In those language there is definitely a difference. Visual Basic 6 for example did not have a short circuit AND operator. VB.NET has introduced one, the 'AndAlso' operator, but the 'And' operator still does not short circuit the evaluation.

However most programming languages would short circuit the evaluation. In that case, the two forms would probably compile to the same code if compiler optimization has been turned on. But that depends on the compiler and optimizer of the compiler.

If compilation optimization has been turned off, I'm guessing that most compilers would not generate the same code for the two forms, as the debugger would need to differentiate between the two separate lines of code.

But as already mentioned, the performance difference of the two forms is unmeasurable, so it is a lot more important that the code communicates its intend than the performance.

share|improve this answer
    
I'm not even aware that VB6 does that kind of evaluation. So does that mean that on VB6 the second example is better when having two conditions? –  MegaNairda May 23 '12 at 9:47
    
"better"? If there is no performance penalty for evaluating the second condition, then the most easy to read code is in my opinion the better option. But there are some cases, where you need to split the if statements, e.g. if you write "if (list != null) && list.Count > 0", as the second condition would be invalid, i.e. throw an exception, if the first condition evaluates to false. –  Pete May 23 '12 at 10:04
    
It's not just VB 6. The & (And) operator does not short circuit on any version of Visual Basic, including the current versions of VB.NET. The only difference is that VB.NET introduced an && operator (AndAlso), which does short circuit. That doesn't exist in VB 6 and earlier versions. –  Cody Gray May 24 '12 at 11:27
    
@CodyGray - Thanks for the update on current VB version. I updated the answer to better reflect this. –  Pete May 25 '12 at 10:39

If working in a compiled language excessive branching can mess with your processor's pipelining so if the 2 logical expressions are simple( e.g. "X<=10 & X >=1") you would be better off not using a short circuiting "and" (&&) or multiple if statements. But some optimizing compilers do this for you.

share|improve this answer
1  
wrong, and no reason given for why you think it's right... –  jwenting Jul 10 at 8:51

In almost every language that's worth its salt the difference will be either zero or practically un-measurable.

The much more important questions are: Which one is more readable? Which one represents the intent more directly?

Readability will be influenced a lot more by these decisions than performance.

share|improve this answer
    
so which is more readable? i'd say the first –  jk. May 23 '12 at 9:35
    
But sometimes having a long one is quite unreadable right? –  MegaNairda May 23 '12 at 9:37
1  
@MegaNairda, then you should extract the long conditional checks into a separate method with a descriptive name. –  Péter Török May 23 '12 at 9:38
    
Personally I find the first one more readable, but it depends a lot on the context. –  Joachim Sauer May 23 '12 at 9:39

In the .NET world, for C# it is better to optimize your if statement, so the left hand operand is the most efficient. Therefore the second one is not executed if it is not needed. Visual Basic will still resolve both, therefore the nested if statement is better for Visual Basic.

share|improve this answer
1  
True, but in Visual Basic you can use operator AndAlso instead of operator And if you want boolean short circuit behaviour (to get the same behaviour as && in some other languages). –  Peter Mortensen Apr 5 at 19:57

With the short circuit && it will be the same code. The only reason you should have to decide is for readability.

Also,

if(condition1){
    if(condition2){

    }
}

will let you have more control over the else cases if they need to be different. If the else for both conditions need to be the same, put them in a single if.

share|improve this answer
    
Another reason could be for debugging (to set breakpoints). –  Peter Mortensen Apr 5 at 20:00

A good optimising compiler should collapse both of these forms into the same machine code.

However, without more information about your environment (and without even being sure which language you are using) it is completely impossible to tell you whether you have a good optimising compiler.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.