does order of the conditions matter on if else statement?

▼魔方 西西 提交于 2020-08-20 09:20:42

问题


On coding bat Logic-2, loneSum problem I typed in the first method listed below. It was right for all of the tests except for only one. The only one it failed on was where all of the variables equaled to the same integer. But when I looked up for the solution online I got the second method listed below and that worked. I think the only difference between my method and the right solution was the position of the "else if(a==b && b==c){return 0;}". My question is does the order of the conditions matter on the "if else" statements? (I apologize in advance for the wording, English is not my first language. Thank You.)

 public int loneSum(int a, int b, int c) {
      int sum = a + b + c;
      if(a==b)
      {return c;}
      else if(b==c)
      {return a;}
      else if(c==a)
      {return b;}
      else if(a==b && b==c)
      {return 0;}
      else
      return sum;
    }

public int loneSum(int a, int b, int c) {
  int sum = a + b + c;

if(a==b && b==c)
  {return 0;}
  else if(b==c)
  {return a;}
  else if(c==a)
  {return b;}
  else if(a==b)
  {return c;}
  else
  return sum;
}

回答1:


Simple sample, 2 same methods, only different order of the if else statements:

String method1(int input) {
    if (input > 1) {
        return "Foo";
    } else if (input > 5) {
        return "Bar";
    }
    return "Baz";
}

String method2(int input) {
    if (input > 5) {
        return "Bar";
    } else if (input > 1) {
        return "Foo";
    }
    return "Baz";
}

Call both methods with an input, lets say 9. Do you think they'll return the same string?




回答2:


Yes, the order of the conditions matters. In your code, you test if(a==b) first. If all 3 integers are the same, then this will be true and only return c; will execute. It won't even bother with the rest of the conditions.

In if and else-if, all conditions are evaluated in order only until the first true condition is found.

Test for the more specific case first, i.e. if(a==b && b==c) first.




回答3:


Surely it does matter.

Take a look on this code snippet:

if (a) {
    doA();
} else if(a && b) {
   doAB();
}

comparing to:

if (a && b) {
    doAB();
} else if(a) {
   doA();
}

Let's assume that both a and b are true.

In first case doA() will be called because we test a first. In second case doAB() will be executed.




回答4:


Hint: what if a == 1, b == 1 and c == 1?

Spoiler: your code returns 1, while the result should be 0.




回答5:


Remember that if/else-if/else executes in order and will stop when it finds an acceptable case. In your example, if a=5, b=5, c=5, you'll return c when you should be returning 0.

This is because a==b and a==b && b==c are not mutually exclusive - they can both be true, so the order matters. If you check a==b first, you'll never get to the more complex check. You'll want to go from most specific check to least specific check.




回答6:


Yes because if the condition in the first if is satisfied it executes the statement in the satisfied if condition and doen't bother about the if else statements beneath it.



来源:https://stackoverflow.com/questions/16406946/does-order-of-the-conditions-matter-on-if-else-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!