Task 1 of 7 · 0 solved · 7 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

2.2 Selection — the else if chain

Lesson

Choosing among many options

One if / else picks between two paths. To pick among several, chain them with else if. Java checks the conditions from top to bottom and runs the first one that is true — then skips the rest.


Worked example

int s = 85;
if (s >= 90) {
    System.out.println("A");
} else if (s >= 80) {
    System.out.println("B");
} else if (s >= 70) {
    System.out.println("C");
} else {
    System.out.println("F");
}

85 is not >= 90, but it is >= 80, so it prints B and stops checking.


▶ Try it: run this, then change s to 95, 72, and 50 — watch which single band prints.



Using the grade chain above, what prints when s is 85?


What prints when s is 72?


Type exactly what the grade chain prints when s is 50:


True or false?


Using the grade chain (A >= 90, B >= 80, C >= 70, else F), sort each score by the grade it prints.