Now, it’s the time to execute the code on Decision Base, i.e. Computer will decide on the basis of certain conditions that which block will be executed. Many a times we also take action on the basis of the decision here let’s take an example of a Traffic Light, our action (stop, wait and go) depends on the light. Similarly, with the help of Selection Flow you will give condition-based instructions to computer also.
Selection Flow depends on the condition, i.e., if condition of a particular expression is True then a set of statements will be executed otherwise, other set of statements will be executed.
But the question is How can we use Selection Flow?
Here, comes the Decision Making Statements to the rescue. Python will support the following types of decision making statements :
- if statements
- if-else statements
- if-elif statements
- Nested if statements
if statement :
It is the simplest form of “if statement” where it tests a condition and if it is true then it will execute the indentation block just below the condition.
Syntax :
if condition :
statement1
statement2
Let’s understand with example, suppose you want to check if you are eligible for voting or not. For this, see the below code :
Now, if someone enter their age greater than or equal to 18 then the condition (line no 2) is true and the block inside if statement i.e., print statement on line no 3 will be executed. But if someone enter their age less than 18 then condition (line no 2) is fail so in this case we haven’t implemented any instruction to the machine and that’s why nothing will be executed.
if-else statement :
In above example, if you want to print something when condition is failing, like you want to print that “you are not eligible” then you should use if-else statement.
Syntax :
if condition :
statement1
statement2
else:
statement1
statement2
Let’s modify above code to understand the requirement of “else” statement.
If someone enter their age less than 18 then condition (line no 2) is fail, else block will execute i.e., line no 5 and you will see the message “You are not eligible”.
The if – elif Statements :
Now, what if you want to check another condition in the case when test condition of “if” evaluates to false, i.e., you want to check a condition before the control reaches to else part. Something that you need to test in the given conditions :
if traffic light is red:
then you should stop
elif traffic light is orange :
then you should wait
else:
then you should go
So, in the above example you need to implement more than 2 conditions, then in this case you always go with if…elif statement. You can use any number of elif statement.
Syntax :
if condition :
statement1
elif condition :
statement1
else:
statement1
Let’s understand with a program, suppose you want to check a number is negative, positive or zero :
Now, in the above program we have 3 conditions to test, one condition is with “if” statement and one with else, now what about 3rd condition? For 3rd condition we use “elif” after if condition. Now, if “if statement” is false then control check “elif” statement” and if this is true then it will execute the block inside it, otherwise it executes “else” block.
The nested if Statements :
Sometimes above discussed form of “if” are not sufficient. We need to test more and more conditions. For this, we use nested if statements.
The nested if statements allow you to check for multiple text expression and executes different codes for more than two conditions. These statements can be used in different category like :
Let’s understand with a program, suppose you want to check that you are eligible for work or not or you are eligible for pension:
When we entered the age of 12 for the above code then, first “If” condition (line no 2) is TRUE so the output is displaying the print statements inside the If statement, i.e.
When we entered 32 as an age then, first “If” condition (line no 3) is FALSE so, it will enter into else block (line no 5), and there it checks for the Nested If condition. Here, the Python Nested If statement (line no 6) is TRUE so, the output is displaying the print statements inside the Nested If statement.
When we entered 65 as an age then, first If condition (line no 3) is FALSE so, it will enter into else block (line no 5), and there it will check for the Nested IF condition. Here Python Nested If statement (line no 6) is also FALSE so, the output is shown in print statements inside the else block (line no 9) of Nested If statement.
Operators with If Statements :
As we studied about a lot of operators (in Operators & Expression), let’s discuss about operator which are mandatory for “if” statements. You can use only “Relational” (<,>, <=,>=, ==, =) as well as “Logical” (and, or, not) Operators with if statement conditions.
So, in above example you saw that each and every form of IF statement use relational operator at the time of condition. But what about logical operator and when we use them?
Logical operators are used in control statements, when you want to check more than one expression in a single statement. They return the result as “True” or “False”.
Let’s understand sequential flow of programs by creating programs.
Write a program to accept 3 numbers and find the greater one.
Before start writing any code, just analyse the program. A program can work in 3 phases i.e., Input, Processing and Output. We consider these 3 phases and find what we need in above program.
These phases are:
- Input : accept three numbers from user as an input
- Processing : Put the formulae (conditions) to check which number is greater.
- Output : Print the greatest number as an output.
Let’s implement the above steps in program :