0

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:

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 anyone can help explain this to me I would greatly appreciate it. Thanks

user2329174
  • 87
  • 1
  • 7
  • Try it and see. – markspace Mar 31 '18 at 15:04
  • ah. I can see in this instance why it doesn't work as 76 is greater than all of them higher than D, but surely there are some scenarios where else/if is not necessary? – user2329174 Mar 31 '18 at 15:06
  • Here's another exercise: Remove the `else` statements, and work out what it would take to fix the output for any input value of `testscore`. – markspace Mar 31 '18 at 15:12

4 Answers4

2

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.

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...

So in your example, not only would you get the wrong result, you would also do a lot more checks!

schneida
  • 729
  • 3
  • 11
  • 37
1

As @schneida already mentioned else if skips the condition check if a previous condition already was true. If the condition is changed in the if case then it is critical to skip the following condition checks:

if (value > 100)
{ 
    value *= 2.0; // get some big bonus
} 
else if (value > 50)
{
    value *= 1.5;
}
milbrandt
  • 1,438
  • 2
  • 15
  • 20
0

Assume testScore is 93. With the above code as mentioned the output would be

Grade = 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. So final answer would be Grade = D Hope it helps you understand.

0

You could eliminate the if and else if branches entirely. Load a Map<Integer, Character> with values covering the decimalized grade ranges (0-10 as F-A). Then divide a given testscore by 10 to determine the correct grade. 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).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249