Task 5 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 — else, the catch-all

Lesson

else: when nothing else matched

A final else (with no condition) runs only when every earlier condition was false. It is the catch-all, so exactly one branch of the whole chain always runs.


Worked example

int n = -4;
if (n > 0) {
    System.out.println("positive");
} else if (n == 0) {
    System.out.println("zero");
} else {
    System.out.println("negative");
}

n is -4: not > 0, not == 0, so the final else runs and prints negative.


▶ Try it: run this, then set n to 0 and to 7 — the final else catches everything the earlier tests missed.



Using the sign chain above, what prints when n is 0?


Type what the sign chain prints when n is 7:


Match each value of n to what the sign chain prints.


True or false?