In this article we are going to learn the first concept of Flow Control and learn how to use the if, elif and else statement statements:

Table of content:

  • What are If and else conditions
  • Python if and else statement
  • Python if, elif and else statements
  • Nested if statements

What are if and else statements?

In python, there is a keyword if which operates on the Booleans operators such as True and False. if statement is used to take the decision whether to execute the block code which is inside the if statement or not.

If statement syntax:

if Expression:
    statement()

the statement() will only execute if the Expression is True. First, the if statement checks whether the statement is True or not.

If the if statement found that the Expression is False then the statement() won’t be executed.

For you Information in python 0 treated as False:

let’s understand it with an Example:

if True:
    print("1 This Statement will execute because the Expression is true")

if False:
    print("2 this statement won't execute because the expression is False")

if  0:
    print("3 this statement won't execute because the expression is False")

if 2 > 3:
    print("4  this statement won't execute because 2 is not greater than 3 and it is False")

if not 2>3:
    print("5 this statement will execute because 2 is not greater than three ")

#Output

1 This Statement will execute because the Expression is true
5 this statement will execute because 2 is not greater than three

In the above example, we wrote 5 if statements and in output, we got only 2 statements which were True

If an if statement does not execute because the Expression and you want another statement to execute there, we use else statement.

An else is a statement works only if the if statement does not execute. This means that if the expression is False then only the else statement works.

The else statement always corresponds to the if statement you cannot use an else statement independently if there is no if statement.

Syntax to write an else statement

if Expression:
    statement( if Expression is True)
else:
    statement(if Expression is False)

Let’s understand it with an example:

if True:
    print("1 this statement will execute because of the if expression is True")
else:
    print("2  this statement will not execute because its corresponds if statement is True")

if 3 > 4 :
    print("3 this statement will not execute because the Expression is False ")
else:
    print("4 this statement will execute because its correspond if statement is False ")

#Output:

1 this statement will execute because of the if expression is True
4 this statement will execute because its correspond if statement is False

In the above example, we saw that only that else statement executes because the if the expression was False. in the above example there we 4 statements to executes but only 2 execute.

And always remember one if statement can have only one else statement and it should be next to it.

If elif and else statements:

Till now we have used if and else now we will learn a new additional statement use with the if statement that is elif. elif stands for else if, though in the above example we saw that the else statement does not take any expression it works according to the expression passed in the if statement. What if we want a statement which takes expression when the if statement expression is False, here we use the elif statement which is used next to the if statement.

Like else statement you cannot use elif statement if there is no if statement. When we use an if statement there can be only one else statement corresponding to if but this is not a case with elif, there could be more than one elif statements that could be corresponded to one if statement.

Syntax to use an elif statement:

if expression:

    statement(if expression is true)

elif another_expression :

    statement(if the expression is false and another_expression is True)

elif another_expression_2 :

    statement( if the expression and another_expression is False and another_expression_2 is True)

else:

    statement( if all the above expression is False)

let’s understand it with an Example:

if 4 < 3 :
    print("the expression of if statement is False so this statement won't execute")
elif 4 < 2:
    print(" this statement won't execute because the elif expression is False ")
elif 2 < 4:
    print("this statement will execute because the expression of this elif statement is True")
else:
    ("this statement will execute if all the above expressions are false")

#Output

this statement will execute because the expression of this elif statement is True

Nested if Statements:

The nested if statement means that we are using an if statement inside the another if statement. When you using the nested if statements be careful with the indentation.

Let’s understand it with an example

x=40
if x > 20:
    print("x is greater than 20 ")
    if x > 30:
        print("(nested if statement) x is greater than 30 too")
    
        if x > 40:
            print("(nested_nested  if statement) x is greater than 40 too")
        else:
            print("(nested_nested else statement) x is not greater than 40")
    
    else:
        print("(nested if statement) x is not greater than 30")
else:
print("x is not greater than 20")

#Output

(nested if statement) x is greater than 30 too
(nested_nested else statement) x is not greater than 40

Points to Remember:

  • The if statement will only execute if its expression is True.
  • If the expression is False the else statement will execute.
  • You cannot use else and elif statements without If statement
  • While using the nested if statements be careful with the indentation

Python Tutorials:

Leave a Reply

Your email address will not be published. Required fields are marked *