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.
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.
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.
The syntax of the nested if…elif…else construct may be:
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
- If Else Statement While
- For (Range Function)
- Nested Loops
- Use of Compound Expression in Conditional and Looping Construct
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:
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.