Facebook Pixelelif Statement | Python Tutorial | CodeWithHarry

elif Statement

Sometimes, the programmer may want to evaluate more than one condition. This can be done using an elif statement.

An elif statement works on the following principle:

  • Execute the block of code inside the if statement if the initial expression evaluates to True. After execution, return to the code outside the if block.
  • Execute the block of code inside the first elif statement if the expression inside it evaluates to True. After execution, return to the code outside the if block.
  • Execute the block of code inside the second elif statement if the expression inside it evaluates to True. After execution, return to the code outside the if block.
    • ...
  • Execute the block of code inside the nth elif statement if the expression inside it evaluates to True. After execution, return to the code outside the if block.
  • Execute the block of code inside the else statement if none of the expressions evaluate to True. After execution, return to the code outside the if block.

Elif Statement Flowchart

Example:

num = 0
if (num < 0):
    print("Number is negative.")
elif (num == 0):
    print("Number is Zero.")
else:
    print("Number is positive.")

Output:

Number is Zero.