Like other programming languages, there are some control flow statements in Python as well. Control flow refers to the order in which the program should be executed. Generally, the control flow of a program runs from top to bottom. However, the control flow statements break the general top to bottom order of execution by including decision making, looping and more. This enables the program to first execute a specific block of the code based on the conditions used.
In this module we will learn about if else control flow statements. Following is the list of all the topics that we will cover, in case you need to jump to a specific one.
So, without any further delay, let’s get started.
Watch this video on Control flow in Python.
Decision making in programming, much similar to decision making in real life, is quite important as it helps us decide what the program should do next. Decision making helps in deciding the flow of execution of the program. In python, decision making is implemented using if…else statements. The conditional logic in Python is primarily based on the “if … else” structure.
Starting from if statements, it is the most basic decision-making statement. It simply decides whether a particular code block will be executed or not on the basis if the condition provided in the if statement. If the condition provided in the if statement is true then the code block is executed and if it’s false then the code block is not executed.
Following flowchart explains the working of if statements in Python:
As depicted by the flowchart above, the python program first evaluates the test expression. It is basically the condition in if statement. If the condition is met or if the condition is true then only the statement(s) in the body of the if statement is executed.
Note: The body of the if statement starts after an indentation unlike other languages that use brackets to write the body of if statements.
Let’s see an example of implementation of if statement.
Learn Python in 16 hrs from experts
Learn Python in 16 hrs from experts
if statements tell the program what to do if the condition is true. In case the condition is false, the program just goes on to execute what comes after if statements. In situations where we want the program to execute some statement if the condition is true and some other statement but only if the condition is false, then we use if…else statements.
Following flowchart explains the working of if else statements.
As depicted by the flowchart above, the python program first evaluates the test expression. It is basically the condition in if statement. If the condition is met or if the condition is true then only the statement(s) in the body of the if statement is executed. If the condition is not true then the statement in the body of the else statement is executed. The body of if and else statements start with indentation.
Let’s see an example of implementation of if… else statement.
Here, the elif stands for else if. This conditional statement in Python allows us to check multiple statements rather than just one or two like we saw in If and If else statements. If the first if condition is true then, same as before in previous if and if else statements, the program will execute the body of if statement. Otherwise the program will go to the elif block (else if) which basically checks for another if statement. Again, if the condition is true the program will execute the body of elif statement, and if the condition is found to be false, the program will go to the next else block and execute the body of else block.
if elif else ladder:
When there is more than just two if conditions in if elis else statements then it is referred to as if elif else ladder, as it resembles the structure of a ladder in terms of if statements. If one of the if conditions turns out to be true then the rest of the ladder is just bypassed and the body of that particular if block is executed. If all the if conditions turn out to be false then the body of last else block is executed.
Following flowchart depicts the working of if elif else statements:
Syntax of if elif else statement
You can put as many elif statements as you program requires before the last else statements, making it an if elif else ladder.
Let’s see the following example of if elif else statement to get a better understanding.
In the above example, the program first checks the very first if statement. Since it turns out to be false, the body of if statement is not executed and the program goes to the next elif statement. It also turns out to be false and again the body of the elif block is skipped and program goes to the next elif statement. Same thing happens here. Since all the conditions were false, program finally reaches the last else statement and executes the body of else. So, we get the output as “value of variable a is greater than 40”.
As the name suggests, nested if statements are nested inside of another if statement. That means that nested if statement is the body of another if statement. We use nested if statements when we need to check secondary conditions only if the first condition executes as true.
Following flow chart depicts the working of nested if statements.
Syntax of nested if:
Let’s see the following example to understand better
Here in this example, since a is 20 the program enters in the first if statement. Then it checks the nested if statement and executes it as true and prints a is smaller than 25 on the screen. Now the nested else and else statements won’t be executed and the program will go to the statement after the end of if block.
Short hand if and if else is nothing but a way to write the if statements in one line when you have only one statement to execute in if block and else block.
Short hand If example:
Short hand if else example:
With this we come to an end of this module. Next module highlights while loops in Python. Stay tuned! Meanwhile, you can take a look at our offer for Python Training Course and a free guide to Python Interview Questions.Previous Next
Download Interview Questions asked by top MNCs in 2019?