Sometimes, even if-else statements don’t do the job correctly. Perhaps we have multiple conditions where each one has a different action corresponding to it. This is where the else if statements come in. The body of a certain else if statement only runs if all the previous conditions were false and the condition of that particular else if statement is true.
As you can infer from that sentence, you can have as many else if statements as you need to cover all the conditions, while there must be only one if statement and at most one else statement.
Altogether, we have an if-else if-else statement, also called a multi-way selection. Here is its anatomy:
some code run before
if (condition1) {
code that runs if condition1 is true
} else if (condition2) {
code that runs if condition2 is true
while condition1 is false
} else if (condition3) {
code that runs if condition3 is true
while condition1 and condition2 are false
} .
.
.
else {
code that runs if none of the
conditions above are true
}
Here, we will code an example to return the largest divisor between 1 and 10 that a number is divisible by:
public static int largestDivisorLessThanTen(int number) {
if (number % 10 == 0) {
return 10;
} else if (number % 9 == 0) {
return 9;
} else if (number % 8 == 0) {
return 8;
} else if (number % 7 == 0) {
return 7;
} else if (number % 6 == 0) {
return 6;
} else if (number % 5 == 0) {
return 5;
} else if (number % 4 == 0) {
return 4;
} else if (number % 3 == 0) {
return 3;
} else if (number % 2 == 0) {
return 2;
} else {
return 1;
}
}
When writing your conditions, remember to write them in the right order! As a rule of thumb, we put the more restrictive conditions before less restrictive ones. In the example above, if we reverse the conditions in this code, then all even numbers would return 2, even if they are also divisible by 4, 6, 8, or 10! It would be helpful to test possible inputs to make sure that you cover all test cases to get the right results and fix the order of your conditionals if need be!
We've been emphasizing this a lot, but now that you know how to combine a bunch of different conditions, it's more important than ever to use brackets and indentation properly. If you don't, the computer really will get super confused about what code it should run. And it will also be a lot trickier for you to go back and figure out what's wrong.
Here is another example that determines if a year is a leap year or not (years divisible by 4 are leap years unless they are divisible by 100, in which case they are leap years only if they are divisible by 400):
public static boolean isLeap(int year) {
if (year % 400 == 0) {
return true;
} else if (year % 100 == 0) {
return false;
} else if (year % 4 == 0) {
return true;
}
return false;
}
Remember that once a return statement is run, the method will end and any remaining code in the method will not be run. That's why we're able to have return false;
at the end of the method. If any of the conditions above were satisfied, we would have already returned something, stopping the method before it reaches the last return false;
.
Notice that we didn't put an else around the last return false;
. In this context, we know that this line is only run if none of the previous conditions were satisfied. This will not always be something we know for certain, so the last line might change what our code does. When in doubt, put an else around code that should only run if the previous conditions were false.