简体   繁体   English

多个else / if语句的目的? 如果不是else语句,则不是一个

[英]purpose of multiple else/if statements? NOT single if then else statement

I just wanted to know if there is any purpose to writing else if or if this is purely written for better code readability. 我只是想知道是否有其他目的,或者是否纯粹为了提高代码的可读性而编写。 For instance, this code taken from the oracle java tutorials: 例如,此代码取自oracle java教程:

class IfElseDemo {
public static void main(String[] args) {

    int testscore = 76;
    char grade;

    if (testscore >= 90) {
        grade = 'A';
    } else if (testscore >= 80) {
        grade = 'B';
    } else if (testscore >= 70) {
        grade = 'C';
    } else if (testscore >= 60) {
        grade = 'D';
    } else {
        grade = 'F';
    }
    System.out.println("Grade = " + grade);
}
}

surely writing the "else if" parts with just if would do that same thing? 肯定用if编写“ else if”部分会做同样的事情吗? If anyone can help explain this to me I would greatly appreciate it. 如果有人可以帮我解释一下,我将不胜感激。 Thanks 谢谢

It's mostly about performance but also about what you want to express! 这主要与性能有关,也与您要表达的内容有关! Imagine the testscore is >= 90 then the grade variable would be set to 'A' and no other if statements would be evaluated. 假设testscore是> = 90,那么等级变量将被设置为“ A”,而其他if语句将不被评估。

Rewriting the code to 将代码重写为

class IfElseDemo {
  public static void main(String[] args) {

  int testscore = 76;
  char grade;

  if (testscore >= 90) {
    grade = 'A';
  } 
  if (testscore >= 80) {
    grade = 'B';
  }
  if (testscore >= 70) {
    grade = 'C';
  } 
  if (testscore >= 60) {
    grade = 'D';
  } else {
    grade = 'F';
  }
  System.out.println("Grade = " + grade);
  }
}

would also check if the testscore is >= 80 and set the grade to 'B', then check for >= 70 and set it to 'C', etc... 还将检查testscore是否> = 80并将成绩设置为“ B”,然后检查> = 70并将其设置为“ C”,依此类推...

So in your example, not only would you get the wrong result, you would also do a lot more checks! 因此,在您的示例中,不仅会得到错误的结果,还会进行更多的检查!

As @schneida already mentioned else if skips the condition check if a previous condition already was true. 正如@schneida已经提到的, else if跳过条件检查,如果先前的条件已经为真。 If the condition is changed in the if case then it is critical to skip the following condition checks: 如果在if情况下条件发生更改,那么跳过以下条件检查非常重要:

if (value > 100)
{ 
    value *= 2.0; // get some big bonus
} 
else if (value > 50)
{
    value *= 1.5;
}

Assume testScore is 93. With the above code as mentioned the output would be 假设testScore为93。使用上面提到的代码,输出为

Grade = A 等级= A

But if you replace "else if" with "if", it will execute all if statements because of which grade is first set to A then to B then to C and finally it will be set to D . 但是,如果将“ else if”替换为“ if”,它将执行所有if语句,因为首先将哪个等级设置为A,然后设置为B,然后设置为C ,最后将其设置为D。 So final answer would be Grade = D Hope it helps you understand. 因此,最终答案将是Grade = D希望对您有所帮助。

You could eliminate the if and else if branches entirely. 您可以完全消除ifelse if分支。 Load a Map<Integer, Character> with values covering the decimalized grade ranges (0-10 as FA). 加载Map<Integer, Character>的值覆盖十进制等级范围(FA为0-10)。 Then divide a given testscore by 10 to determine the correct grade. 然后将给定的测试分数除以10以确定正确的成绩。 Like, 喜欢,

Map<Integer, Character> map = new HashMap<>();
for (int i = 0 ; i < 6 ;i++) {
    map.put(i, 'F');
}
map.put(6, 'D');
map.put(7, 'C');
map.put(8, 'B');
map.put(9, 'A');
map.put(10, 'A');

for (int testscore = 100; testscore >= 0; testscore--) {
    int v = (int) (testscore / 10.0);
    System.out.printf("%d %c%n", testscore, map.get(v));
}

Outputs all possible scores and their results without any if (s). 输出所有可能的分数及其结果,而不包含任何if

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

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