-1

Is there a difference between using nested if statements and using one long if statement where there are a lot of arguments?

e.g.

if ( var1 == var2 && var3 == var4 )
{
  //do something
}

or

if ( var1 == var2 )
{
  if ( var3 == var4 )
  {
    //do something
  }
}

I would like to know if there is any difference in doing things this way?

thanks

Master Yoda
  • 4,334
  • 10
  • 43
  • 77
  • 1
    You have more flexibility in the second format. Suppose there's another condition that should be only examined if var1 == var2. If you used the first format, you would have to check var1 == var2 all over again. – Solace Mar 09 '14 at 22:05
  • In many languages these both codes would be compiled into same binaries. Also in most cases there is no need to create additional indentation. I would use nested `if` in this form for example if there would be an `else` statement which would depend only on `var3 == var4` condition. – Pshemo Mar 09 '14 at 22:06
  • It's all about readability and coding style. Try to keep the code simple and readable. Performance-wise, both statements are equal, due to [lazy evaluation](http://en.wikipedia.org/wiki/Lazy_evaluation). – Warlord Mar 09 '14 at 22:06
  • possible duplicate of [Invert "if" statement to reduce nesting](http://stackoverflow.com/questions/268132/invert-if-statement-to-reduce-nesting) – thudbutt Mar 09 '14 at 22:07
  • @thudbutt not quite... – Master Yoda Mar 09 '14 at 22:12
  • 2
    The question is both an opinion-based question and an example of language tag spam. What is true in one language (such as optimization of execution) may not necessarily be true in another. – Hovercraft Full Of Eels Mar 09 '14 at 22:12
  • I Just tested with php and to my surprise the nested if is faster if the first test is true and the second test is false 1.5secs and 1.1secs for 17mil loops. All other tests were equal. 1.5secs 1.1secs – Steven Martin Mar 09 '14 at 22:55

5 Answers5

2

In general, the compiler will generate the same or nearly the same code for these two variants. For interpreted languages that support short-circuiting (not evaluating second part of an || or && when the outcome has been determined), it will also be pretty much the same thing.

My advice would be to use the form that represents what you want to do most clearly.

For example, if we want to check if two coordinates match:

if (x1 == x2 && y1 == y2) 

Buf if your code is something like this:

if (country_of_origin == "Sweden")
{
    if (today == thursday)
    {
       ... 
    }
}

may make more sense...

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
2

The second style allows for checking that needs to be done independently of the first if condition.

For example, if you needed to do something in an extra condition where (var3 != var4) BUT (var1 == var2) still, it would be impossible to do with:

"if(var1 == var2 && var3 == var4)"

Thus, you would need something like:

if(var1 == var2){
    if(var3 == var4){

    }

    //more code...
    //... var3 & var 4 change...
    //

    if(var3 != var4){

    }
}

As the commenter @Solace said, it simply allows for more flexibility.

h4x0rjax
  • 359
  • 1
  • 6
  • 15
1

They both do the same thing , but personally I think nested is neater and it also allows code to be executed on either side of the nested if, which is usually required

if ( var1 == var2 )
{
     //do something here based on the first if but before second if

  if ( var3 == var4 )
  {
    //do something 
  }
    //do something here also which may be different based on the nested if result
}


But if you dont need this , then your first code is probably a tiny bit faster with just a single if

Steven Martin
  • 3,150
  • 1
  • 20
  • 27
1

Different situations require different solutions:

if ( width == height && width == depth )
{
  // Its a cube!
}

if ( x == y )
{
  if ( p == q )
  {
    //do something for when x == y but p == q
  } else {
    //do something for when x == y but p != q
}

You cannot generalise for what constructs are should or should not be used - they are all appropriate in some situation.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

Well, it depends what is your condition. If it's same as yours, than there is absolutely no need to choose the second way.

It's about writing clean and readable code - doing this like if (condition1 && condition2) you are suggesting that both conditions are absolutely necessary to the following fragment of code.

Eel Lee
  • 3,513
  • 2
  • 31
  • 49