samedi 31 mars 2018

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:

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

Aucun commentaire:

Enregistrer un commentaire