Conditional Control Structures In Python Programming

Introduction

 
A conditional control structure is used to execute statement(s) based on some condition. When the condition is associated with a statement(s) that is true only then we want to execute the related/associated statement(s) otherwise, we want to ignore/skip those statement(s). If we want the execution of the statement(s) or skipping of the statement(s) to happen only once based on the outcome of the condition, it's preferred to use the conditional control structures. In Python programming, indentation is the key factor.
 
Therefore, we need to indent the statement(s) properly. We must also note that Python programming language is case-sensitive and most of the keywords are in lowercase ( like the keywords if and else must be written in lower case only).
 
In Python programming language we have the following 4 conditional control structures.
  • Simple if-statement.
  • If-else statement.
  • If-elif-else statement.
  • Nested if statement.

The general syntax of a simple if statement

 
if (<condition-1>):
    statement-1
    statement-2
statement-p
 
Whenever we have a condition to be tested and we need to execute statement(s) based on a condition then we can use a simple if statement. In the above syntax if the condition-1 evaluates to true then statement-1 and statement-2 will be executed, otherwise, sequential execution continues from statement-p onwards. 
 

General syntax of if....else statement

 
if(<condition-1>):
    statement-1
    statement-2
else:
    statement-3
    statement-4
 
Whenever we have a situation where we want to test a condition and if the condition is true we want to execute some statement(s) and when the condition is not true we want to execute another set of statements, we must use the if-else statement. As per the above syntax of the if-else statement, when condition-1 evaluates to true the statement-1 and statement-2 will be executed and, when the condition-1 evaluates to false the statement-3 and, statement-4 will be executed. Therefore either of the statement-1 and statement-2 or statement-3 or statement-4 will be executed but not both.
 

General syntax of if....elif.....else statement

 
if(<condition-1>):
    statement-block-1
elif(<condition-2>):
    statement-block-2
elif(<condition-3>):
    statement-block-3
.....................
.....................
elif(<condition-n>):
    statement-block-n
[else:
    statement-block-p]
statement-q
 
When we have more than one condition to be tested then we must use the if-elif-else statement. The construct of an if-elif-else statement begins with an if-statement and we can write any number of times elif-statements followed by a final else-statement (which is optional).
 
The execution model of the if-elif-else statement is as follows. It will start by testing the condition-1 in case the condition-1 evaluates to true then the statement associated with the first condition (statement-block-1) will alone be executed and after that, the control will branch to statement-q which is outside the if-elif-else construct. In case the condition-1 evaluates to false, then the second condition (condition-2) is tested and, in case the condition-2 evaluates to true then the statement-block-2 alone will be executed, after which the control will branch outside the if-elif-else statement (that is to statement-q). In case the control reaches condition-n it means that the previous n-1 condition evaluated to false and if the condition-n evaluates to true then the statement-block-n will be executed otherwise statement-block-p will be executed. An if-elif-else construct can be used to avoid multiple if statements as well.
 

The general syntax of a nested-if statement

 
Whenever we write an if-statement inside another if-statement or if-statement inside an else-statement is known as a nested-if.
 
Sample syntax #1
 
if(<condition-1>):
    if(<condition-2>):
        statement-1
        statement-2
 
Only if condition-1 and condition-2 both evaluate to true, then statements 1 and 2 will be executed, otherwise not.
 
Sample syntax #2
 
if(<condition-1>)
    statement-1
    statement-2
else:
    if(<condition-2>):
        statement-3
        statement-4
 

Summary

 
Here we have an if-statement inside else-statement and, this is also known as a nested if statement. If condition-1 evaluates to false and condition-2 evaluates to true only then statements 3 and 4 will be executed. In case condition-1 evaluates to true then statements 1 and 2 will be executed.