“else if()” versus multiple “if()”s in C#

后端 未结 10 1786
一个人的身影
一个人的身影 2021-01-03 21:51

How do these practically differ?

// Approach one
if (x == 1)
    DoSomething();
else if (x == 2)
    DoSomethingElse();

// Approach two
if (x == 1)
    DoSo         


        
相关标签:
10条回答
  • 2021-01-03 22:33

    Note that there is no else if construct in C#. Your first code sample is exactly the same as:

    if (x == 1)
        DoSomething();
    else 
    {
        if (x == 2)
            DoSomethingElse();
    }
    

    Since there is only one statement in else, the braces can be omitted, and for enhanced readability, if is usually written on the same line as the preceding else. Writing multiple "else if" statements is equivalent to further nesting:

    if (x == 1)
        DoSomething();
    else 
    {
        if (x == 2)
            DoSomethingElse();
        else
        {
            if (x == 3)
                YetSomethingElse();
            else
            {
                if (x == 4)
                   ReallyDifferent();
            }
        }
    }
    

    The above can be written as:

    if (x == 1)
        DoSomething();
    else if (x == 2)
        DoSomethingElse();
    else if (x == 3)
        YetSomethingElse();
    else if (x == 4)
        ReallyDifferent();
    

    From this, you can see that chaining "else if" and if can produce different results. In case of "else if", the first branch that satisfies the condition will be executed, and after that no further checks are done. In case of chained if statements, all branches that satisfy their conditions are executed.

    The main difference here is when execution of a branch causes a subsequent condition to become true. For example:

       var x = 1;
    
       if (x == 1)
           x = 2;
       else if (x == 2)
           x = 3;
    

    VS

       var x = 1;
    
       if (x == 1)
           x = 2;
    
       if (x == 2)
           x = 3;
    

    In the first case, x == 2, while in the second case x == 3.

    0 讨论(0)
  • 2021-01-03 22:34

    If x is modified by multiple threads it is possible that DoSomething() and DoSomethingElse() will both get called with the second approach

    0 讨论(0)
  • 2021-01-03 22:37

    Else if will only be evaluated if the first condition wasn't true. But two consecutive if statements will both be evaluated.

    You can easily test this concept in a Python interpreter:

    First run this:

    Note: In Python elif is used instead of else if

    a = 1
    if a >= 1:
      print('a is greater than or equal to 1')
    elif a<=1:
      print('a is less than or equal to 1')
    

    then run this

    a=1
    if a >= 1:
      print('a is greater than or equal to 1')
    if a<=1:
      print('a is less than or equal to 1')
    
    0 讨论(0)
  • 2021-01-03 22:39

    When you code like this

    // approach two
    if (x == 1)
        DoSomething();
    if (x == 2)
        DoSomethingElse();
    

    Everytime the condition checks.

    But when you code like this

    if (x == 1)
        DoSomething();
    else if (x == 2)
        DoSomethingElse();
    

    If the first condition is true then it wont check next else if condition and thus decrease unnecessary compiling.

    0 讨论(0)
  • 2021-01-03 22:39

    If x changes (in Do Seomthing and DoSomethingElse) then the first statement will only ever execute one statement. In the 2nd example every statement will be checked (unless of course the compiler optimizes it to a jump table for number comparison).

    0 讨论(0)
  • 2021-01-03 22:43

    If DoSomething sets x to 2, then they will differ.

    0 讨论(0)
提交回复
热议问题