EMZETT.
Login

else…if

In short: A chain of several conditions checked in sequence — as soon as one is true, its block runs and all further ones are skipped.

In more detail: Syntactically, else if is not its own Java keyword, just a nested if within the else branch of the previous if. The order matters: the first matching condition “wins”, even if a later one would also match.

if (grade == 1) System.out.println("Excellent");
else if (grade == 2) System.out.println("Good");
else System.out.println("Satisfactory or worse");

In Depth

int grade = 3;
if (grade == 1) {
    System.out.println("Excellent");
} else if (grade == 2) {
    System.out.println("Good");
} else if (grade == 3) {
    System.out.println("Satisfactory");
} else if (grade >= 4 && grade <= 6) {
    System.out.println("Insufficient");
} else {
    System.out.println("Invalid grade");
}

Because only the first matching condition is executed, the ORDER is crucial for overlapping ranges — if (grade >= 1) ... else if (grade >= 4) ... would never reach the second branch, because every number ≥ 4 is already also ≥ 1 and thus already lands in the first branch. For many consecutive else if branches that all check the same value against fixed constants (as in the example above), a switch statement is often more readable and sometimes more performant, because the compiler can partly generate a jump table for it instead of sequential comparisons.

Nested conditions vs. else-if chains

else if checks conditions SEQUENTIALLY and independently of each other — every condition is evaluated completely fresh, regardless of what was checked before. This differs from truly nested if blocks, where an inner condition only makes sense in the context of the outer one:

// else-if: independent, sequential checks
if (age < 0) System.out.println("Invalid");
else if (age < 18) System.out.println("Minor");
else if (age < 65) System.out.println("Adult");
else System.out.println("Senior");
 
// True nesting: the inner condition only makes sense in context
if (isLoggedIn) {
    if (hasAdminRights) {
        System.out.println("Admin area");
    }
}

Switch expressions as a modern alternative

Since Java 14, switch expressions offer a more compact way to write exactly the case where each branch simply returns a different value — the -> arrow replaces the error-prone break of classic switch statements and prevents accidentally “falling through” into the next case:

String category = switch (grade) {
    case 1, 2 -> "Good";
    case 3, 4 -> "Passed";
    default -> "Failed";
};

For range checks (< 18, >= 65, etc.), an else if chain still remains necessary, since classic switch constructs in Java only support exact value comparisons against constants, not ranges.

Common pitfall: missing else at the end

An if/else if chain with no closing else doesn’t necessarily cover all cases — if no condition matches, simply nothing happens, which is easy to overlook:

int grade = 7; // outside the expected range 1-6
if (grade == 1) System.out.println("Excellent");
else if (grade == 2) System.out.println("Good");
// no else - for grade=7, NOTHING AT ALL is printed, with no error message

A closing else as a “catch-all” for unexpected values makes such gaps visible instead of silently ignoring them.

See also: if…else, if Statement, Switch Statement