In this article, we will going to learn about decision making statement of python programming language.
In some situations the programmers have to make decisions and the next block of code will we executed based on these decisions. Here in python, the if...else...elif and nested if statement is used as the decision making statement.
Python if statement
This is the most simplest decision making statement of python programming language. This statement is used when the programmer has to decide whether to execute a certain block of code or not. It means that if a conditions is true then the block of code will be executed and if it is false then it will not be executed. In python, the non zero values are interpreted as true and the 0 and None are interpreted as False.
Syntax
if expression:
statements
Flow Chart of Python if statement
Python if statement Example ;
a = 10b = 5
if a > b :
print(" A is greater term ")
Output
A is greater term
Python if...else statement
If...else statement is used by the programmers to execute a block of code in a situation where the condition is false. The if...else statement is used to evaluate the test expression and if the condition is to then the block of code will be executed but if the condition is false, then the body of else will be executed.
Syntax
if expression:
statements
else:
statements
Flow Chart of python if...else statement
Python if...else statement Example;
a = 5b = 10
if a > b :
print(" A is greater term ")
else :
print(" B is greater term ")
Output
B is greater term
Python if...elif...else statement
In python, if...elif...else statement is used when the programmer have to multiple expressions. When the condition of if is false then the next elif block is checked and if it is also false then next block is checked and so on. And if all the conditions comes out as false then the body of else will be executed. Here, in if block we can have multiple elif block but only one else block.
Syntax
if expression:
statements
elif expression:
statements
else:
statements
Flow Chart of python if...elif...else statement
Python if...elif...else statement Example;
a = 200if a > 100 :
print(" A is greater than 100 ")
elif a < 100 :
print(" A is less than 100 ")
elif a == 100 :
print(" A is 100 ")
else :
print(" A is not a number ")
Output
A is greater than 100
Python Nested if statement
When we writes a if...elif...else statement is written inside the other if...elif...else statement then it is known as nested if statement. Simply, we can say that in python, we can easily place a statement inside the other statement.
Syntax
if condition:
if condition:
statements
else:
statements
else:
statements
Flow chart of python Nested if statement
Python Nested if statement Example;
x = 5if (x == 5):
if (x < 10):
print("The x is smaller than 10")
if (x < 7):
print("The x is smaller than 7 too")
else:
print("The x is greater than 10")
Output
The x is smaller than 10
The x is smaller than 7 too
Conclusion
Above we have discussed about decision making statement of python programming language. In some situations the programmers have to make decisions and the next block of code will we executed based on these decisions. In python, the if...else...elif and nested if statement is used as the decision making statement.