简体   繁体   English

If Else VS Multiple If语句

[英]If Else VS Multiple If Statements

I always use multiple if statements when coding: 我总是在编码时使用多个if语句:

if(logicalCheck){
  ...
}

if(secondLogicalCheck){
  ...
}

and rarely use If Else . 很少使用If Else I understand that using my way means that more than one of my logical checks can be fulfilled and only one in an if else chain can occur. 我知道,使用我的方式意味着可以完成我的多个逻辑检查,并且只有if if else链中的一个可以发生。

My question is, is there any performance benefits in using one method over the other in C, Java or JavaScript? 我的问题是,在C,Java或JavaScript中使用一种方法相对于另一种方法是否有性能优势? Is there anything particular wrong with using multiple if statements? 使用多个if语句有什么特别的错误吗?

Using only if , interpreter or whatever will test all conditions but if you use else if (if possible) then on passing any condition , next else if will not be tested or checked. 仅使用if ,解释器或其他任何东西可以测试所有条件,但是如果(如果可能)使用else if则在通过任何条件else if将不测试或检查else if条件。

if (age < 10){
   // do something
}
if (age < 20 && age > 10){
   // do something
}
if (age < 30 && age > 20){
   // do something
}

All conditions will be tested/compared 所有条件都会经过测试/比较

but in this scenario 但是在这种情况下

if (age < 10){
   // do something
}
else if (age < 20 && age > 10){
   // do something
}
else if (age < 30 && age > 20){
   // do something
}

if age is 5, only first condition will be tested. 如果年龄为5岁,则只会测试第一个条件。

If at most one of the conditions is expected to be true, using if else-if will save the evaluation of some of the conditions. 如果最多期望其中一个条件为真,则使用if else-if将保存对某些条件的评估。 Since the conditions may be expensive to evaluate, evaluating multiple conditions without an actual need to do so may cost you performance wise. 由于评估条件可能会很昂贵,因此无需实际评估多个条件可能会降低性能。

If more than one condition can be true at the same time, the decision whether to use multiple if statements or a single if - else-if .. else construct depends on the required logic - ie do you want more than one of the blocks accessed by the multiple conditions to be executed if more than one condition is true. 如果可以同时满足多个条件,则决定使用多个if语句还是使用一个if-else-if .. else结构取决于所需的逻辑-即,您是否要访问多个块?如果多个条件为真,则执行多个条件。

The term of use is this: 使用期限是这样的:

If you have multiple independent logics which is used for non-related restrictions or actions upon the condition then you can use multiple if statement separately: 如果您有多个独立的逻辑用于条件的不相关限制或操作,则可以分别使用多个if语句:

if(conditionA){
    // do what suppose to do
}

if(conditionB){
    // do what suppose to do
}

. . .


If you want one of the conditions you made to apply then you should use if else or if else if statemnets: 如果要应用其中一种条件,则应使用if elseif else if state ifnets:

if(conditionA) {
        // do what suppose to do on conditionA
} else {
    // do what suppose to do if the conditionA doesn't satisfied.
}

if(conditionA) {
        // do what suppose to do on conditionA
} else if(conditionb) {
        // do what suppose to do on conditionB
}  else {
    // do what suppose to do if non of the conditions were satisfied.
}

By the way if you want to use if else if chain it's better to use switch case statements: 顺便说一下,如果要使用if else if链,最好使用switch case语句:

switch(true){
    case conditionA:
        // do what suppose to do on conditionA
        break;
    case conditionB:
        // do what suppose to do on conditionB
        break;
    default:
    // do what suppose to do if non of the conditions were satisfied.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM