Else If Chains
An else if chain checks several alternatives from top to bottom. The first true condition wins, and the rest of the chain is skipped.
Thresholds should usually be written from highest to lowest. If score >= 60 came first, every score above 60 would take that branch before reaching the stronger ratings.
Check thresholds in order
public class Main {
public static void main(String[] args) {
int score = 82;
if (score >= 90) {
System.out.println("Excellent");
} else if (score >= 75) {
System.out.println("Good");
} else if (score >= 60) {
System.out.println("Developing");
} else {
System.out.println("Needs support");
}
}
}
Output:
Good
A score of 82 fails the 90 check and then matches the 75 check, producing Good.
Use a final else when every possible value needs an outcome. Without it, some inputs may quietly do nothing.
Classify a number as positive, negative, or zero.