The if-elif-else Statement

The if-else statement handles two sides of the same condition: True and False. This works very well if we’re working with a problem that only has two outcomes. However, in programming, it isn’t always a True or False scenario, and a problem can have multiple outcomes. For instance, a grading program may have only pass or fail outcomes, but more often there are multiple different grades that are possible. This is where the if-elif-else statement shines. It is the most comprehensive conditional statement because it allows us to create multiple conditions easily. The elif stands for else if, indicating that if the previous condition fails, try this one.

Structure

The if and else blocks will remain the same. The elif statement comes in between the two.

Get hands-on with 1400+ tech skills courses.