The first true branch wins
In an else if chain only the first true condition counts, so the order of the conditions changes what the program does. Look at this chain, meant to give a grade:
int points = 95;
if (points >= 50) {
System.out.println("Passed");
} else if (points >= 90) {
System.out.println("Excellent");
}
Passed95 should be Excellent, but points >= 50 is checked first, it's true, and the rest of the chain is skipped. Excellent can never be printed. Checking the strictest condition first fixes it:
int points = 95;
if (points >= 90) {
System.out.println("Excellent");
} else if (points >= 50) {
System.out.println("Passed");
}
ExcellentWhen conditions overlap, put the most specific one first. Each else if can then rely on the earlier conditions having been false: in the second version, the >= 50 branch only ever sees points below 90.