Conditional Constructs and Looping

Nested If

In the programming languages, a lot of the times you will see that in a large project, one has to control the flow of execution of their program. Moreover, one wishes to execute some set of statements only if the given condition is satisfied. Also, a different set of statements when it is not satisfied. We will go through nested if in this article.

nested if

Conditional Statements in Python

We refer to conditional statements are as decision-making statements. Moreover, we need to make use of these conditional statements for executing the specific block of code if the specified condition is true or false.

In Python we can accomplish decision making by putting into use the statements given below:

  • if statements
  • if-else statements
  • elif statements
  • Nested if and if-else statements
  • elif ladder

This article will help you learn about the nested if statements with some real-time examples.

Nested If

Thus, you see that there may arise a situation when one wishes to check for another condition after a condition resolve to true. In this kind of situation, one can make use of the nested if construct. To be more specific, in a nested if construct, one can have an if…elif…else construct inside another if…elif…else construct.

Syntax

The syntax of the nested if…elif…else construct may be:

if expression1:

statement(s)

if expression2:

statement(s)

elif expression3:

statement(s)

elif expression4:

statement(s)

else:

statement(s)

else:

statement(s)

Example

                    

var = 100

if var < 200:

print "Expression value is less than 200"

if var == 150:

print "Which is 150"

elif var == 100:

print "Which is 100"

elif var == 50:

print "Which is 50"

elif var < 50:

print "Expression value is less than 50"

else:

print "Could not find true expression"

print "The End!"

When the above code is executed, it produces the following result −

The expression value is less than 200

Which is 100

The End!

Browse more Topics Under Conditional Constructs and Looping

Nested If Statements

One can place an if (or if…else, if…elif) statement inside another statement. Therefore, you see that we refer to this process as nesting. Moreover, it allows one to make complex decisions on the basis of varied inputs. Given below is a sample syntax:

if condition:

if condition:

statements

else:

statements

else:

statements

You will notice how the lines above are indented. Notably, the indentation is significant as it forms Python that the indented statement is the second-level statement.

                    

Take a look at the example given below:

x = int(input(‘Enter your age: ‘))

x = int(input('Enter your age: '))

if x > 19:

if x > 95:

print('You are too old, go away!')

else:

print('Welcome, you are of the right age!')

else:

print('You are too young, go away!')

You will notice how the program above prompts the user to enter their age. First, the program will determine if the user is older than 19 in age. If that is the case, then the program will moreover check if the user is older than 95.

After that, if that is the case, it will be printing the corresponding message which will denote that the user is too old. Similarly, if the user enters a number between 19 and 95, the ‘Welcome, you are of the right age!’ message will get printed.

Alternatively, if the user enters a number less which is than 19, they will be notified of being too young. Most importantly, the nested if…else statement will not execute at all if the user decides to enter a value less than 19.

So, the output of the example above will be as the following:

                    

Enter your age: 42

Welcome, you are of the right age!

>>>

Enter your age: 10

You are too young, go away!

>>>

Enter your age: 99

You are too old, go away!

>>>

Please remember that one can also nest a statement inside a nested statement. You simply need to make sure to indent the code properly.

FAQ on Nested If

Question 1: Can you do nested if statements in python?

Answer 1: Yes, you can. A nested if is an if statement that is the target of another if statement. Nested if statements refers to an if statement inside another if statement. In other words, yes. Python does allow you to nest if statements within if statements.

Question 2: Can a for loop be nested in an IF statement?

Answer 2: Yes, it can. One can put an if statement inside a for loop. Please remember that your syntax for the for loop is invalid though, not the if statement even though the code analyser may wrongly accuse the if statement.

Question 3: What do you mean by a nested if Elif else statement?

Answer 3: The if-elif-else statement comes into use for conditionally executing a statement or a block of statements. Further, conditions may either be true or false. Moreover, it can also execute one thing when the condition is true ad something else when the condition is false.

Question 4: Is Elif a nested IF?

Answer 4: Elif is indented more than if, before another block encloses it. Thus we consider it inside the if block. Moreover, since inside the if there isn’t any other nested if, the Python interpreter regards elif as a syntax error.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.