flowchart TD A[Input first number x] --> B[Input second number y] B --> C{Is x > y?} C --> |Yes| D[x is greater than y] C --> |No| E[x is less than y]
Flowchart to find greater of the two numbers.
Many a times in the life cycle of a program there are instances when you would like to run a certain code based on whether a particular condition is met or not. A real-world example of this would be screening of content based on the user’s age. If, e.g., the user is 18+ then all the content is accessible to the user while if the user is below 18 then certain content is restricted. These kind of testing of condition(s) and then executing the specific code accordingly is facilitated by the conditional statements.
if
and else
statementsTo test a particular condition, if
statement is used with some condition that we need to check. This kind of checking generally involves a comparison operator such that the output is always Boolean (true/false). In case the output of the comparison is true the code block following the if
statement gets executed. On the other hand, if the condition is false then the code specified under the else
block gets executed. Note that there is no testing of condition in case of else
statement since the else
directly means when the if
condition is false.
When using conditional statements, there are two syntactical points that you need to keep in mind:
if
and the else
statements ends with a colon (:).if
and else
have to be indented (i.e. leading spaces at the begining of the line).flowchart TD A[Input first number x] --> B[Input second number y] B --> C{Is x > y?} C --> |Yes| D[x is greater than y] C --> |No| E[x is less than y]
Flowchart to find greater of the two numbers.
= 5
x = 6
y
if(x>y):
print("X is greater than Y")
else:
print("X is less than Y")
X is less than Y
elif
Multiple conditions can be tested as well the elif
keyword. The syntax for this keyword is similar to the if
keyword i.e. elif
also takes a condition within parenthesis. The else
keyword take no argument such that its code block gets executed when the if
condition (and elif
) conditions are return False
.
flowchart TD A["Input first number x"] --> B[Input second number y] B --> C{Is x > y?} C --> |Yes| D[x is greater than y] C --> |No| E{Is x less than y?} E --> |Yes| F[x is less than y] E --> |No| G[x is equal to y]
Flowchart to compare two numbers.
if(x>y):
print("X is greater than Y")
elif(x<y):
print("X is less than Y")
else:
print("X is equal to Y")
For situations when there is a need to check multiple conditions in tandem, we can have nested conditional statements. This is different from the elif
statement since here we need to check certain condition after some condition has already been tested to be true (or false depending upon the code). For instance, let’s say, we want to write a program that checks whether a number is even or odd and, if even, check whether the number ends with a zero.
%%{init: {'theme':'neutral'}}%% graph TD A[Input a number x] --> B{Is x even?} B --> |Yes| C{Does x ends \n with a zero?} B --> |No| D[x is odd] C --> |Yes| E[x is even and ends with a zero.] C --> |No| F[x is even and does not ends with a zero.]
Flowchart for nested conditions.
= 50
x if(x % 2 == 0):
if (x % 10 == 0):
print("The number is even and ends with a zero.")
else:
print("The number is even and does not ends with a zero.")
else:
print("The number is odd.")
The number is even and ends with a zero.
There are situations when multiple conditions need to be tested simultaneously. For such cases, we can use the Boolean operators – and
, or
, and not
– with the if
statement. To understand this let’s write a code to test, given two numbers, which number is greater and is the difference between the two numbers greater than 10.
%%{init: {'theme':'neutral'}}%% flowchart TD A["Input first number x"] --> B[Input second number y] B --> C{Is x > y? \n Is their difference \n greater than 10?} C --> |Yes| D[x is greater than y \n and \n their difference is greater than 10.] C --> |No| E[Either x is less than y \n or \n their difference is less than 10.]
Flowchart for comparison with Boolean logic.
= 20
x = 5
y
if((x > y) and (x-y > 10)):
print("x is greater than y and the difference in x and y is more than 10.")
else:
print("Either x is less than y or the difference in x and y is less than 10 or both.")
x is greater than y and the difference in x and y is more than 10.
Quiz: Write a program to check if a number is completely divisible by five.
= 15
x if(x % 5 == 0):
print("The number is completely divisible by five")
else:
print("The number is not completely divisible by five")
Quiz: Given a date of birth, write a program to check in which quater of the year the person was born.
# Note that the date of birth should be in a specific format e.g. DD/MM/YYYY.
= "14/02/2000"
DOB = int(DOB.split("/")[1]) #change to integer data type.
month if(month < 4):
print("The person was born in the first quater")
elif(month > 3 and month < 7):
print("The person was born in the second quater")
elif(month > 6 and month < 10):
print("The person was born in the third quater")
else:
print("The person was born in the fourth quater")
in
with conditionalsSo far we have seen the use of conditional operators for framing the if
statements. When working with lists (or for that matter strings) we can use the in
keyword to check for the presence of a particular element (or character).
= ["Apple", "Mango", "Banana"]
fruits if("Mango" in fruits):
print("Make a mango shake")
else:
print("No mango shake")
Make a mango shake
= "abcde"
x if("c" in x):
print("c is there")
else:
print("c is not there")
c is there
We can also construct the conditional statements using functions that return a Boolean value.
= "Apple"
x if(x.startswith("A")):
print("The first alphabet of the variable x is A")
else:
print("The first alphabet of the variable x is not A")
The first alphabet of the variable x is A
# The isinstance function checks if an object is an instance of a given class.
= 5.0
y if(isinstance(y, int)):
print("y is an integer")
else:
print("y is not an integer")
y is not an integer