else if chains
With more than two cases, chain conditions with else if. Java checks them from the top, runs the block of the first condition that's true, and skips the rest. A final else catches everything else.
int temperature = 12;
if (temperature > 25) {
System.out.println("Hot");
} else if (temperature > 15) {
System.out.println("Warm");
} else if (temperature > 5) {
System.out.println("Cool");
} else {
System.out.println("Cold");
}
CoolFor 12, the first two conditions are false and the third is true, so the program prints Cool and the rest of the chain is skipped. Exactly one block of the chain runs. The final else is optional; without it, no block runs when no condition is true.