Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A better alternative to using a long series of "if/else if" statements in C?

Tags:

c

if-statement

Originally I used switch/case, but the condition had to be a constant value that the variable would match, versus a boolean of whether the variable was within the range.

Instead, I have this monstrosity:

    if ( data[y] > 91 ) {
       grades[9] = grades[9] + 1;
    }
    else if ( data[y] > 88 && data[y] < 92 ) {
        grades[8] = grades[8] + 1;
    }
    else if ( data[y] > 84 && data[y] < 89 ) {
        grades[7] = grades[7] + 1;
    }
    else if ( data[y] > 81 && data[y] < 85) {
        grades[6] = grades[6] + 1;
    }
    else if ( data[y] > 79 && data[y] < 82) {
        grades[5] = grades[5] + 1;
    }
    else if ( data[y] > 74 && data[y] < 79 ) {
        grades[4] = grades[4] + 1;
    }
    else if ( data[y] > 71 && data[y] < 75 ) {
        grades[3] = grades[3] + 1;
    }
    else if ( data[y] > 68 && data[y] < 72 ) {
        grades[2] = grades[2] + 1;
    }
    else if ( data[y] > 59 && data[y] < 69 ) {
        grades[1] = grades[1] + 1;
    else {
        //data[y] < 60:
        grades[0] = grades[0] + 1;
    }

Does anybody know a nicer way to handle this block of code, since my switch/case idea can't apply? Surely there has to be a better way to do this.

like image 657
HB- Avatar asked Sep 11 '14 18:09

HB-


People also ask

What can I use instead of if-else in C?

While polymorphism is an advanced object-oriented-programming (OOP) topic in C++, it can also be a helpful alternative to if-else statements. In C++, developers can implement structures where different functions get called on an object, depending on that object's class.

What is an alternative type of statement to a series of IF THEN ELSE statements?

An alternative to IF-THEN-ELSE I use often is the use of logical expressions. A logical expression is specified within parentheses '()' and are evaluated as being true or false.

What can I do instead of a bunch of if statements?

Sometimes a switch case is exactly what you need. You should almost always use a switch case if it can replace multiple if statements.

Which is equivalent to multiple if-else if statements?

In this case, when one IF statement is used inside another IF statement, this is called the nested IF statement. This allows to use more than one condition simultaneously.


3 Answers

The most obvious to shorten your code is to get rid of the unnecessary second tests:

if      (data[y] >= 92) ++grades[9];
else if (data[y] >= 89) ++grades[8];
else if (data[y] >= 85) ++grades[7];
else if (data[y] >= 82) ++grades[6];
else if (data[y] >= 80) ++grades[5];
else if (data[y] >= 75) ++grades[4];
else if (data[y] >= 72) ++grades[3];
else if (data[y] >= 69) ++grades[2];
else if (data[y] >= 60) ++grades[1];
else                    ++grades[0];
like image 138
ooga Avatar answered Apr 02 '23 23:04

ooga


This seems like a sensible data-driven approach:

int grade_cutoff[] = { 59, 68, 71, 74, 79, 81, 84, 88, 91, INT_MAX };
int grade_bucket;

for (grade_bucket = 0; data[y] > grade_cutoff[grade_bucket]; grade_bucket++) {
  /* nothing */
}

grades[grade_bucket]++;
like image 38
hobbs Avatar answered Apr 02 '23 21:04

hobbs


Use a loop. You'll also need an array of the minimum data value for each grade. Here's some untested code as an example:

for (int i = 9; i >= 0; i--)
{
    if (data[y] >= minDataValueForGrade[i])
    {
        grades[i]++;
        break;
    }
}

It's short, easy to read, and makes it really easy to change the values that correspond to each grade.

like image 26
shoelzer Avatar answered Apr 02 '23 21:04

shoelzer