-2

I would like to now what are (in theory) the differences between using

if (a == x && b == y)

and

if (a == x){
    if (b == y){

    }
}

Edit : My question is not about the result but more about the performances. I find out that sort a table then search something into it is more efficient that do a research directly. I would like to know if there is a same thing with this, or if the compiler just replace the 'and' by another if

Thank you

Marsgames
  • 113
  • 9
  • 4
    There are no differences. Try it out. –  Mar 17 '18 at 21:50
  • For that instance there would not be. It would be different if there would be a possible null reference exception in `b` I am pretty sure – Jon Wisniewski Mar 17 '18 at 21:52
  • 2
    If you have else clause somewhere then it will have a difference but for now there is no difference – jose_bacoy Mar 17 '18 at 21:52
  • In this case there really isn't a difference except you are going inside the `a==x` scope and you can run code that just pertains to the `a==x` in there, or both. If you replaced the `&&` with `||` then there would be a bigger difference. – Ron Beyer Mar 17 '18 at 21:53
  • what makes you think there's a difference? – Marc Gravell Mar 17 '18 at 21:53
  • 1
    Possible duplicate of [Replacing Nested if Statement With AND](https://stackoverflow.com/questions/11351214/replacing-nested-if-statement-with-and) –  Mar 17 '18 at 21:53
  • My question is not about the result but more about the performances. I find out that sort a table the search something into it is more efficient that search directly. I would like to know if there is a same thing with this, or if the compiler just replace the 'and' by another if – Marsgames Mar 17 '18 at 21:58
  • 1
    `more about the performances` Gotcha, so your underlying problem / concern is performance. In that case, have a read of https://ericlippert.com/2012/12/17/performance-rant/ . Or if your are interested in what the compiled code looks like, have a play with ILSpy - https://marketplace.visualstudio.com/items?itemName=SharpDevelopTeam.ILSpy . – mjwills Mar 17 '18 at 22:00
  • Makes your code more readable – Markiian Benovskyi Mar 17 '18 at 22:07
  • I will do this. But as it was something like "for the culture", I would have liked to know if it is admit that there is a real difference, and then it's useless to do some tests to check, or if there's "nothing about it" and everything can alter the result. Now, about the article, I don't think that I have to look for performance only when there is an issue, but it's just my opinion – Marsgames Mar 17 '18 at 22:08
  • Since they both act the same way, yeah it likely makes sense @Marsgames to do it the way that is more consistent with your team and the existing codebase. For _most_ teams the first option would be preferred in most instances (due to conciseness and less indentation for the code inside the `{` `}`) - but not universally. – mjwills Mar 17 '18 at 22:14
  • 2
    && is a short-circuiting operator. It completes skip the right-hand side expression when the left one is false. That is not a feature of the CLR nor the jitter, it is something the C# compiler has to do. You've been thinking about how it *might* do it. Yes, exactly like that. So perf is a non-issue, it generates the same code. Feel free to use the more compact version. If you want to think some more about it, consider if & might be a better choice. Hint: it often is. – Hans Passant Mar 17 '18 at 22:15
  • Thank you @HansPassant. I think I have the answer to my question, "it generates the same code". Thank you all for your help :) – Marsgames Mar 17 '18 at 22:22

3 Answers3

0

When you use && logical operator if the first expresion it's false the second expresion will not be evaluated. So in theory both examples are the same.

-1

In first case:

if (a == x && b == y)
{
    // code block
}

the code block never execute unless the two condition a == x && b == y are true

but in second case:

if (a == x)
{
    // code block 1

    if (b == y)
    {
        // code block 2
    }
}

the code block 1 will execute when only the first condition a == x is true but the code block 2 will execute when two condition a == x && b == y are true

MrMustafa
  • 305
  • 3
  • 16
  • I don't understand why this is down voted. Sure the original question does not mention interjecting code before the inner condition, but this explanation answers why someone would choose to write equivalent looking code in one way vs the other - which is more useful than just saying they are equivalent – whodini9 Mar 17 '18 at 22:47
  • @whodini9 The question is asking about differences of performance. This does not address that question. –  Mar 18 '18 at 16:42
-1

If(a == x && b== y)

1)In this condition both the value should be true means they have to satisfy the condition, then and only then the if condition is gonna satisfy.

On the other hand :

If(a == x){

   If( b == y){

       }
}
  • In this case if the first condition pass means (a == x)satisfy then we enter into if condition and then we check for second condition (b == y). Means it works in sequence. You can also perform some functionality after satisfying the first condition.
vishal rana
  • 1
  • 1
  • 1