Home > Software engineering >  How do I reduce "else if" statements?
How do I reduce "else if" statements?

Time:12-20

Is there a way to reduce the "else if" statements? I have been tasked to make a program that will ask the user to input 3 grades then determine the average of those numbers, and that would also output the equivalent grade of the computed average based on the table that they have given.

here is my code so far.

if (ave >= 98){
    System.out.println("Your grade is 1!");
}
else if (ave >= 95){
    System.out.println ("Your grade is 1.25!");
}
else if (ave >= 92){
    System.out.println ("Your grade is 1.5!");
}   
else if (ave >= 89){
    System.out.println ("Your grade is 1.75!");
}
else if (ave >= 86){
    System.out.println ("Your grade is 2!");
}     
    

CodePudding user response:

If there is smooth function (for each 3 point difference in ave, 0.25 points is added to grade), the grade value should be computed with regard to the minimum 86 and maximum 98 checked values:

static double getGrade(int ave) {
    final int min = 86;
    double grade = 2.0;
    
    int p = (ave - min) / 3;
    p = p < 0 ? 0 : (p > 4 ? 4 : p); // acceptable ranges
    grade -= p * 0.25;
    
    System.out.println ("Your grade for ave = "   ave   " is "   grade   "!");
    return grade;
}

Tests:

getGrade(99);
getGrade(94);
getGrade(86);

Output:

Your grade for ave = 99 is 1.0!
Your grade for ave = 94 is 1.5!
Your grade for ave = 86 is 2.0!

For the updated formula provided in the comments:

1.0 = 98 – 100 %, 1.25 = 95 – 97 %, 1.5 = 92 – 94 %, 1.75 = 89 – 91 %, 2.0 = 86 – 88 %, 2.25 = 83 – 85 %, 2.5 = 80 – 82 %, 2.75 = 77 – 79 %, 3.0 = 75 – 76 %, 5.0 = Below 75 % average

The code needs to be modified to provide values below 75 and between 75..77:

static double getGrade(int ave) {
    double grade = 5.0;
    if (ave >= 75) {
        grade = 3.0;
        final int stepAve = 3;
        final int min = 77;
        final int ranges = 7;
        
        int p = ave < min ? -1 : (ave - min) / stepAve;
        p = p < 0 ? -1 : (p > ranges ? ranges : p); // acceptable ranges
        grade -= (p   1) * 0.25;
    }
    
    System.out.println ("Your grade for ave = "   ave   " is "   grade   "!");
    return grade;
}    

Tests:

for (int i = 72; i < 100; i  = 3) {
    getGrade(i);
}

Results:

Your grade for ave = 72 is 5.0!
Your grade for ave = 75 is 3.0!
Your grade for ave = 78 is 2.75!
Your grade for ave = 81 is 2.5!
Your grade for ave = 84 is 2.25!
Your grade for ave = 87 is 2.0!
Your grade for ave = 90 is 1.75!
Your grade for ave = 93 is 1.5!
Your grade for ave = 96 is 1.25!
Your grade for ave = 99 is 1.0!

CodePudding user response:

In cases like this you can extract the variables that changes, put them on a collection, and loop over the collection. In your case I would do something similar:

int[] threshold = new int[]{98,95,..};
String[] descr = new String[]{"1","1.25",..}

for(int i = 0; i < threshold.length; i  ) {
    if(ave >= threshold[i]) {
        print("Your grade is "   descr[i]);
        break;
    }
}

CodePudding user response:

For multiple if..else if..else . it is always advisable to use switch statement.

public void DisplayGrade(int range)
{
    switch (range)
    {
        case  >= 98 :
          System.out.println ("Your grade is 1!");
        break;

        case >= 95:
           System.out.println ("Your grade is 1.25!");
        break;
        case  >= 92 :
           System.out.println ("Your grade is 1.5!");
        break; 
        case >= 89 :
           System.out.println ("Your grade is 1.75!");
        break;
        case >= 86:
           System.out.println ("Your grade is 2!");
        break; 
    }
} 

CodePudding user response:

I would recommend a NavigableMap to map the lower average to the grade.
Something like

/** Maps lower average to corresponding grade. */
static final NavigableMap<Integer, String> GRADES =
    Collections.unmodifiableNavigableMap( new TreeMap<>( Map.of(
        98, "1", 
        95, "1.25", 
        92, "1.5", 
        89, "1.75", 
        86, "2",
        Integer.MIN_VALUE, "not specified"    // avoid null check
    )));

String grade(int average) {
    return GRADES.floorEntry(average).getValue();
}

The last entry is just for the case that the average is less than 86, obviously it can be changed as needed. Also possible to have Doubles instead of Strings in the map.

CodePudding user response:

The only way you can reduce the number of else-ifs is to cut off after the point where unique outputs are needed and add an else statement to avoid inputs that fall outside your specified ranges:

if (ave >= 98){
    System.out.println("Your grade is 1!");
}
else if (ave >= 95){
    System.out.println ("Your grade is 1.25!");
}
else if (ave >= 92){
    System.out.println ("Your grade is 1.5!");
} 
else { //for example. if you do not need a custom output range below avg of 92...
    System.out.println("Your grade is {whatever else it can be}");
}

As pointed out in comments, your code is already simplified enough. You can try to use a switch statement but I don't think it is worth the trouble since your code should work fine and isn't taking unneeded steps. All you need is an else statement at the end.

  • Related