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.
else block in Pythn
An else statement follows an if statement. An else block contains that codes which will be executed if the condition inside if block fails. The general prototype of else block is:
# Example of if-else block with single line of code to execute if <condition to check>: Code to execute else: Code to execute
# Example of if-else block with multiple lines of code to execute if <condition to check>: Code to execute Code to execute Code to execute else: Code to execute Code to execute Code to execute
Like if blocks, in case of else blocks also we have to keep some point in mind while using it:
- Every else block must follow an if block. If there is an else block without a corresponding if block then we will get an error. As example:
# This approach is correct if <condition to check>: Code to be executed else: Code to be executed # All the below approaches will give us error # case - 1 (only else block, no if block) else: Code to be executed # case - 2 (else block not corresponding to if block) if <condition to be checked>: Code to be executed Code to be executed else: Code to be executed # case - 3 (wrong whitespace indentation) if <condition to check>: Code to be executed else: Code to be executed
- The codes which are going to be executed inside an else block must be properly indented with whitespace. Otherwise, we will not get our desired output or we may get an error. As example:
if <condition to be checked>: Code to be executed # Properly indented Code to be executed # Properly indented Code to be executed # Properly indented else: Code to be executed # Properly indented Code to be executed # Properly indented Code to be executed # Properly indented Code to be executed # Not properly indented Code to be executed # Not properly indented
- The else statement should end with a colon (:) in an else block. If we don’t do so, we will get an error.
if <condition to check>: Code to be executed else: # This is the correct approach Code to be executed #________________________________________________________________ if <condition to check>: Code to be executed else # This is the wrong approach Code to be executed # In this case we will get error
- There can be only one else block in connection with an if block. If we give more than one else block within an if block we will get an error.
Now let’s take a simple example of an if-else block:
a=int(input("Enter an Integer Number : ")) if(a>0): print("The number "+str(a)+" is greater than zero") else: print("The number "+str(a)+" is less than zero")
As output we will get:
# Assume that we have run the code two times #___________________________________________ # First time assume we have given input 6 >>>Enter an Integer Number : 6 The number 6 is greater than zero #___________________________________________ # Second time assume we have given input -6 >>>Enter an Integer Number : -6 The number -6 is less than zero
Nested if-else blocks – elif
The elif statement (in other languages it is known as else-if) is a shortcut to use when chaining multiple if and else statements. Like if block, in the elif block also a condition is placed. If that condition satisfies, then the codes regarding that particular elif block will be executed. A series of if and elif statements may have or may not have a final else block. If there is a final else block is placed, then that block will be called if the conditions of the if and elif blocks are not satisfied.
The general prototype of Nested if-else block is:
# Example of if-else blocks with a single line of code to execute if <condition to check>: Code to be executed elif <condition to check>: Code to be executed elif<condition to check>: Code to be executed else: # This else block is not mandatory Code to be executed # We can have as many number of elif block as we want, but all of them must be # in connection with an if block
# Example of if-else blocks with multiple lines of code to execute if <condition to check>: Code to be executed Code to be executed Code to be executed elif <condition to check>: Code to be executed Code to be executed Code to be executed elif<condition to check>: Code to be executed Code to be executed Code to be executed else: # This else block is not mandatory Code to be executed Code to be executed # We can have as many number of elif block as we want, but all of them must be # in connection with an if block
Now let’s take an example of nested if-else blocks:
a=int(input("Enter an Integer Number : ")) if(a>0): print("The number is greater than 0") elif(a<0): print("The number is less than 0") else: print("The number is 0)
As output, we will get:
# Assume that we have run the code 3 times #_________________________________________ # Case - 1 # Assume that we have given 5 as input >>>Enter an Integer Number : 5 The number is greater than 0 #_________________________________________ # Case - 2 # Assume that we have given -5 as input >>>Enter an Integer Number : -5 The number is less than 0 #_________________________________________ # Case - 1 # Assume that we have given 0 as input >>>Enter an Integer Number : 0 The number is 0 #_________________________________________
Note that, if the conditions within the if or elif statement fails, then the block of code with that if or elif block will not be executed. But we will not get an error for that.