

On the other hand, if outer if condition is evaluated to False, then execution of statements inside the outer if block is skipped and control jumps to the else (line 9) block to execute the statements inside it. If it is False then the control comes out of the if-else statement to execute statements following it and nothing is printed. If it is True then "Congratulations you are eligible for loan" is printed to the console. In the outer if block gre_score > 150 condition is tested. In this case the inner if statement belong to the outer if block and the inner if block has only one statement which prints "Congratulations you are eligible for loan".įirst the outer if condition is evaluated i.e per_grad > 70, if it is True then the control of the program comes inside the outer if block.

The inner if statement is known as nested if statement. Here we have written a if statement inside the another if statement.

Gre_score = int ( input ( "Enter your GRE score: " )) per_grad = int ( input ( "Enter percent secured in graduation: " )) if per_grad > 70 : # outer if block if gre_score > 150 : # inner if block print ( "Congratulations you are eligible for loan" ) else : print ( "Sorry, you are not eligible for loan" ) For example, is not the part of the if block, as a result, it will always execute no matter whether the condition in the if clause is true or false. The statements followed by the if clause which are not indented doesn't belong to the if block. On the other hand, if the condition is false then all the statements in the if block is skipped. If condition is true then all the statements inside the if block is executed. When if statement executes, the condition is tested. We use this recommendation in all our programs throughout this course. Python documentation recommends to indent each statement in the block by 4 spaces. Each statement inside the if block must be indented by the same number of spaces. Many languages like C, C++, Java, PHP, uses curly braces ( ) to determine the start and end of the block but Python uses indentation. Notice that each statement inside the if block is indented by the same amount to the right of the if keyword. When a block of statements is followed by if clause, it is known as if block. A block is simply a set of one or more statements. In the next line, we have have block of statements. Nested If Else Statement Exampleįollowing is the example of a nested if-else statement in python to execute the required block of statements based on the defined Boolean expression.The first line of the statement i.e if condition: is known as if clause and the condition is a boolean expression, that is evaluated to either True or False. If you observe the above nested if-else statement flow chart diagram, we added an if-else statement inside another if-else statement to execute the block of statements based on our requirements. Python Nested If Else Flow Chartįollowing is the flow chart diagram of the nested if-else statements process flow in python. To know more about specifying the indentation in python, refer to Python basic syntaxes. If you observe the above nested if-else statement syntax, we created an if-else statement inside another if-else statement.Īs discussed in python if and python if-else statements, you need to follow the indentation while creating the nested if-else block statements otherwise, you will get build errors ( IndentationError).
