The `elif` and `ifelse` Statements

Learn about `elif` and `ifelse` syntax.

We'll cover the following

So far with our conditional statements, we had an if statement and an else statement. This limits us to only two options, with each if statement evaluating either True or False outcomes. There are a lot of scenarios where it is necessary to check more than two possible outcomes. To overcome this challenge, programming languages offer the solution of an elseif statement in PowerShell, or an elif in Python.

These statements are just like an if statement whose whole purpose is to evaluate another condition. We can write as many elseif or elif statements as we need in the program, but there can be only one if and one else statement.

elif in Python

Syntax of if..elif..else in Python:

if expression1:
    statement
elif expression2:
    statement
elif expression3:
    statement
else:
    statement

Let’s take a simple example of how to determine the grade of a student. We will define multiple conditions using elseif and elif conditional statements in PowerShell and Python respectively to check the range in which the student’s percentage falls in.

Get hands-on with 1200+ tech skills courses.