Python – if…else statement

In this article, we will learn how to alter the flow of code based on the conditional expression using the if…else statement.

While developing applications, for a programmer, it is essential to know how to control the flow of code using conditional expressions. In the previous article, we discussed how we could use if-statement to conditionally execute a suite ( a group of statements) in the if-block. Like, if the condition provided in the if-statement evaluates to True, then run the code in if block, otherwise skip the code in if block i.e.

Flow chart of if-statement in python

But what for the scenarios where the condition in if-statement evaluates to False?

We don’t want to skip to the code after the block. Like we want to run some statements if and if the only condition in the if-statement evaluates to False. Here comes the else keyword in the picture.

Flow chart of if..else in Python

After the if-block ends, we can define an else block with some statements (a suite). The code in the else block will only execute if the condition in if-statement evaluates to False.

if else flow chart in python

Syntax of if..else statement,

if condition:
    statement 1
    statement 2
    statement 3
else:
    statement 4
    statement 5
    statement 6

As soon as the interpreter encounters the if statement, it evaluates the condition in the if-statement, and if that condition evaluates to True, then it executes the suite in if-block, i.e., statements in the if-block and skips the statements in else part.

Whereas, if the condition in if-statement evaluates to False, then it executes the suite in else block directly, i.e., statements in the else block and skips the statements in if block.

Let’s look at some example of if..else

Examples of if…else statement in python

Example 1:

x = 15

print('Change the flow')

if x > 10:
    print('x :: ', x)
    x = x + 1
    print('x :: ', x)
else:
    print('x is fine')
    print('No need to change the x')

print('Last statement')

Output:

Change the flow
x ::  15
x ::  16
Last statement

Here the first two statements were executed in a sequence. But after that, the interpreter encountered an if-statement and then evaluated the condition in that if-statement. As x is 15, which is greater than 10, so the condition evaluated to True. Therefore, the interpreter executed the statements in the if-block and skipped the lines of code in the else-block.

After running the lines in the if-block, it directly jumped to the statements after the else block. The condition in the if-statement evaluated to True, so it executed the code in if-block only and skipped the else-block.

Let’s look at another example of if..else statement

Example 2:

x = 7

print('Change the flow')

if x > 10:
    print('x :: ', x)
    x = x + 1
    print('x :: ', x)
else:
    print('x is fine')
    print('No need to change the x')

print('Last statement')

Output:

Change the flow
x is fine
No need to change the x
Last statement

Here the first two statements were executed in a sequence. After that, the interpreter encountered an if-statement and then evaluated the condition in it. As x is seven, which is smaller than 10, so the condition is evaluated to False. Therefore, the interpreter skipped the statements in the if block. Instead, it directly jumped to the else block to ran the statements in it. After that, it executed the last statement, which is after the if-else.

If the condition in if-statement evaluates to False, then the code in the else block gets executed, and code in if-block gets skipped.

Conclusion:

So, this is how we can use the if…else statement in python to write a code in which the flow of execution can be controlled and directed to any one of the two directions, based on the condition results.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top