Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Find what you need to study

3.4 Else If Statements

4 min readdecember 19, 2022

Athena_Codes

Athena_Codes

Athena_Codes

Athena_Codes

Else If Statements

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 only runs if all the previous conditions were false and the condition of that particular 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 and at most one else statement.

Altogether, we have an if-else if-else statement, also called a . Here is its anatomy:

some code run before if () { code that runs if is true } else if () { code that runs if is true while is false } else if () { code that runs if is true while and are false } . . . else { code that runs if none of the conditions above are true }

Example: Divisibility Counter

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 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.

Example: Leap Year Decider

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 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.

Key Terms to Review (8)

condition1

: Condition1 refers to a logical expression that is evaluated to determine if a certain action should be taken. It is typically used in control structures like if statements and loops.

condition2

: Condition2 refers to another logical expression used in control structures. It helps determine whether an action should be executed based on specific criteria.

condition3

: Condition3 refers to yet another logical expression used in programming. It helps determine whether certain actions should be performed based on specific conditions being met.

else if statement

: An else if statement is used when there are multiple conditions to be checked after an initial "if" condition, and each condition has its own block of code to execute.

if statement

: An if statement is a programming construct that allows the execution of a block of code only if a certain condition is true.

Indentation

: Indentation refers to adding spaces or tabs at the beginning of lines of code to visually organize and structure it. In Python, indentation plays an important role in defining blocks of code within control structures like loops and conditionals.

multi-way selection

: Multi-way selection refers to situations where there are more than two possible outcomes or paths based on different conditions being evaluated.

Return Statement

: A return statement is used in functions/methods to specify what value should be sent back as output when the function is called. It terminates the execution of a function and returns control back to where it was called from.

3.4 Else If Statements

4 min readdecember 19, 2022

Athena_Codes

Athena_Codes

Athena_Codes

Athena_Codes

Else If Statements

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 only runs if all the previous conditions were false and the condition of that particular 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 and at most one else statement.

Altogether, we have an if-else if-else statement, also called a . Here is its anatomy:

some code run before if () { code that runs if is true } else if () { code that runs if is true while is false } else if () { code that runs if is true while and are false } . . . else { code that runs if none of the conditions above are true }

Example: Divisibility Counter

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 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.

Example: Leap Year Decider

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 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.

Key Terms to Review (8)

condition1

: Condition1 refers to a logical expression that is evaluated to determine if a certain action should be taken. It is typically used in control structures like if statements and loops.

condition2

: Condition2 refers to another logical expression used in control structures. It helps determine whether an action should be executed based on specific criteria.

condition3

: Condition3 refers to yet another logical expression used in programming. It helps determine whether certain actions should be performed based on specific conditions being met.

else if statement

: An else if statement is used when there are multiple conditions to be checked after an initial "if" condition, and each condition has its own block of code to execute.

if statement

: An if statement is a programming construct that allows the execution of a block of code only if a certain condition is true.

Indentation

: Indentation refers to adding spaces or tabs at the beginning of lines of code to visually organize and structure it. In Python, indentation plays an important role in defining blocks of code within control structures like loops and conditionals.

multi-way selection

: Multi-way selection refers to situations where there are more than two possible outcomes or paths based on different conditions being evaluated.

Return Statement

: A return statement is used in functions/methods to specify what value should be sent back as output when the function is called. It terminates the execution of a function and returns control back to where it was called from.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.