So from here is the intresting part,
We are going to see what are conditional statements and how to use them in our program.
So what is a condition?
in our programing norms we call conditional statements as yes or no question, which means
that the question we ask must result either True or False only.
In our previous blog we have seen that the relational statements result in either True or
False , Hence we use relational in our conditional statements
Ok, Now let's see what are conditional statements..
In programming, a conditional statements is nothing but a statement which
changes the
flow of control according to the contdition given
Got confused ? let's see an example ...
consider you need to write a program for checking elegiblity for voting
So, the condition here is age >=18
if the age is greate than 18 then print you are elegible for voting
if not then print you are not elegible for voting
SYNTAX:
if (condition):
statements
else:
statements
so let's write the code,
-
age = int(input('enter your age :'))
-
if age >= 18:
-
print('you are eligible for voting')
-
else:
-
print('you are eligible for voting')
So let's see what we have done ..
In the first line we get an input from user and converting to integer as input()
returns string datetype and we are storing the integer value in the variable age
in the second line we are checking for the condition ,
if (age >= 18), then the if block gets executed .
Okay ... so what is a block ?
A block is nothing but a group of statements which has same number of spaces or tabs.
Normally we use {} in some languages to represent a block. But in python,
we use spaces or tabs to represent a block , and the spacing is known as indendation .
In the program, we have indented the print statement below the if statements
so it is called if-block and the print statements below the else is called else block.
Now coming to the condtion , if the condition returns True, then the if block will get
executed (i.e the program will print 'you are elegible for voting') .
If the condition returns False , then the else block gets executed (i.e the program will
print 'you are not elegible for voting')
Hence, here we have two print statements, which gets executed according to the
condition, and this is the power of print statements .
But what if we need to check more than one condition? Let's consider an example ... you are
going to a roller coaster ride , but they said that, you need to be above 150 cm
or weight less than 70kgs in order to go for ride. So here we have two conditions, right?
Here comes the if-elif-else structure
SYNTAX:
if (condition 1):
statements
elif (condition 2):
statements
.
.
.
else:
statements
As you can see we have condition 1, condition 2, .... and at last we have else block
which gets executed if none of the above conditions ge's satisfied.
Let's write the program
-
height = int(input('enter your height in cm :' ))
-
weight = int(input('enter your weight in kg :'))
-
if (height >= 150):
-
print('you can ride!')
-
elif (weight <= 70):
-
print('you can ride')
-
else:
-
print('sorry!, you are not eligible')
Here we have two condition ... one is if the height is greater than 150 then it prints
'you can ride!' as output. The sencond condition gets executed only if the above condition
is not satisfied and the current conndition is satisfied (i.e if we give height as 140
and weight as 60 the if-block is skipped and the elif-block gets get executed ).
If I give the height as 140 and weight as 80 ,then both condition is skipped and
the last statement (i.e. else block) gets executed and prints 'sorry!, you are not eligible'.
As a conclusion , the conditional statement are used to execute a certain
block according to the given condition.