Introduction to Nested IF Statement in Python
Programming has surely come a long way from a simple “Hello World” program to all the modern day’s complex programs. With time new features got added to programming to cater to various needs.
While programming, we often come across situations where certain conditions’ fulfilment would determine our program’s course of action.
So we need to introduce some sort of Decision making capability to our programs to make them more robust.
Let us suppose we have a program in which we input a number from the user and check whether it is an even number or an odd number. It is a classic example of using a conditional statement. The following flowchart would help depict it more clearly:-
Examples of Conditional Statements in Python
This is an example of an elementary conditional statement for finding whether a number is even or not.
The following are the different kinds of Conditional Statements in Python: –
- if statement
- if-else statement
- else-if statement
- nested if statement
- switch statement
In this article, we would focus mainly on Nested If Statements, for which we would have a little introduction of If Statements, and then we would jump to the main topic.
1. If Statements
if <expr>: <statement>
Let us look into the details of the syntax of the if-statement.
- The most important component is the “if” keyword, which helps us identify an expression to be a conditional statement.
- expr: – This signifies the condition, the fulfillment of which would execute the below statement. The expr is basically a Python statement that results in a Boolean value (True or False). The statement for the particular expr will get executed only if the value for the expr is True.
- statement: – This is the final part of the if-statement, which is the path along which the program must flow if the expr is True.
This is just a recap of the if-statement in Python as the nested if is an extension of the same.
2. Nested If Statements
A nested if is an if statement that is the target of a previous if statement. Let us look at the syntax of a nested if-statement.
if <expr1>: <statement1> # Executes statement1 when expr1 is True if <expr2>: <statement2> # Executes statement2 when expr2 is True # Inner if-block ends here # Outer if-block ends here
Let us look at the flow chart of nested if-statement to have a better understanding of it: –
In the following example, we implement the nested if-statement on a program where the user inputs a number and checks different conditions with regards to that number. Though the program is very simple, the only intention is to discuss the nested if-statements’ flow.
a = 10 if(a>5): print("Inside initial if") print("Number is greater than 5") if(a>=10): print("Inside first nested if") print("Number is greater than or equal to 10") if(a>=15): print("Inside second nested if") print("Number is greater than or equal to 15") print("Outside second nested if") print("Outside second nested if") print("Outside initial if")
The nested if must be properly indented, the failure of which results in Indentation Error as follows: –
a = 10 if(a>5): if(a>7): print("Greater than 7")
Colon (:) must be follow all the ifs in the nested if-statements; otherwise, invalid syntax error would occur as follows:
a = 10 if(a>5): if(a>7) print("Greater than 7")
It is finally time to draw closure to this article. In this article, we learnt the need for Conditional Statements in programming, touched upon the basics of if-statement with its syntax. Lastly, we discussed about nested if-statements in Python in details. I hope by now you guys are able to appreciate the usage of nested if-statements in Python. Now it is time to write your own Python program implementing the concepts learnt in this article.
This is a guide to the Nested IF Statement in Python. Here we discuss the Nested IF Statement in Python detailed discussion with examples and its Code Implementation. You can also go through our other suggested articles to learn more –