Loading, please wait...

if..else statements

The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.


Syntax:

if (condition):

    # Executes this block if

    # condition is true

else:

    # Executes this block if

    # condition is false

Example:

i = 20;

if (i < 14):

    print ("i is smaller than 14")

    print ("if Block")

else:

    print ("i is greater than 14")

    print ("else Block")

print ("i'm not in if and not in else Block")

 

Output:

i is greater than 14

else Block

i'm not in if and not in else Block