If Block – Control Structure In Python – Free Python Tutorials

Control Structure In Python – To process the inputs as per user’s requirement, there are some conditional statements available in Python like other languages. They are:

  • if, else and elif block
  • for loop
  • while loop
  • Boolean logic
  • Operator precedence

We will discuss the above points one by one.


if, else and elif block

These are one of the most fundamental conditional statements in any programming language. The main concept behind the if, else or elif is if one or more certain condition/conditions hold then only the code inside that block will be executed, and if the condition/conditions fail then that block will not be executed.


if block

The general prototype of if block is:

# If there is only one line to execute inside if block

if <condition will be here>:
    code_1    # Codes that will be executed if the condition satisfies will be here

# If there are multiple lines to execute inside if block

if <condition will be here>:
    code_1    # Codes that will be executed if the condition satisfies will be here
    code_2    # Codes that will be executed if the condition satisfies will be here
    code_3    # Codes that will be executed if the condition satisfies will be here
    code_4    # Codes that will be executed if the condition satisfies will be here

Here we need to keep two points in mind while working with if blocks:

  • In Python, instead of curly braces, whitespace indentation is used at the beginning of a line to delimit blocks of code. Carefully observe the second case in the above example (when there are multiple lines to execute inside one if block), we have used proper whitespace indentation at the beginning of a line.
    For some more clear concepts, observe the example below:

    # Case - 1
    
    if <condition will be here>:
        code_1    # This code is inside if block
        code_2    # This code is inside if block
    code_3    # This code is outside if block
    code_4    # This code is outside if block
    
    # Case - 2
    
    if <condition will be here>:
        code_1    # This code is inside if block
        code_2    # This code is inside if block
        code_3    # This code is inside if block
        code_4    # This code is inside if block

    From the above example, I hope it is clear how to use whitespace indentations to delimit blocks inside a conditional statement.

  • if statement will be followed by a colon (:) in every if block. If we don’t give the colon after the if statement then we will get an error. As example:
    # This is the correct declaration of if statement
    
    if <condition will be here>:    # We have provided the colon (:)
        code to execute
    
    # This is the wrong declaration of if statement
    
    if <condition will be here>     # We have not provided the colon (:)
        code to execute
    
    # We will get error in the second case

     

Now, let’s take a simple example of if block:

Case – 1:

# Example of if block with a single line to execute

a=int(input("Enter an integer number : "))

# We have taken an integer as user input and typecasted the value to an integer
# Now we are declaring the if blocks

if(a>0):
    print("The number "+str(a)+" is positive")

if(a<0):
    print("The number +"str(a)+" is negative")

if(a=0):
    print("The number "+str(a)+" is zero")

As output we will get:

# Assume we have given 5 as input

>>>Enter an integer number : 5
The number 5 is positive

Case – 2:

# Example of if block with multiple codes to execute

a=int(input("Enter an integer number : ")

if(a>0):
    print("The number "+str(a)+" is positive")
    print("This line is just for demo")

if(a<0):
    print("The number "+str(a)+" is negative")
    print("This line is just for demo")

if(a=0):
    print("The number "+str(a)+" is zero")
    print("This line is just for demo")

Observe the whitespace indentations inside if block carefully. As output we will get:

# Assume we have given -5 as input

>>>Enter an integer number : -5
The number -5 is negative
This line is just for demo

Nested if blocks

We use nested if blocks for more complex condition checkings. In case of nested if blocks, multiple if statements are used and they are placed one inside other. The general prototype of nested if blocks are:

# When there is only one line to execute in every if block

if <condition>:
    Code to execute
    if <condition>:
        Code to execute
        if <condition>:
            Code to execute

# We can use as many if blocks as we want, there is no restriction
# When there is multiple lines to execute in every if block

if <condition>:
    Code to execute
    Code to execute
    Code to execute
    if <condition>:
        Code to execute
        Code to execute
        Code to execute
        if <condition>:
            Code to execute
            Code to execute
            Code to execute

# We can use as many if blocks as we want, there is no restriction

Note that, in both cases, we have given proper whitespace indentations. If we don’t do so either we will get an error or we will not get our desired output (depending on the codes).

Now let’s take a simple example of nested if blocks:

a=int(input("Enter an integer number : ")
if(a>0):
    print("The number is greater than zero")
    if(a<10):
        print("The number is less than 10")

Here we are considering if blocks with a single line only. As output we will get:

# Assume we are giving 6 as input

>>>Enter an integer number : 6
The number is greater than zero
The number is less than 10

Note that, if the condition in any if statement fails, then the codes inside that if block will not be executed, but we will not get any error.


This was all about if blocks in Python. I hope you like this tutorial.

Leave a Comment

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