Skip to content

Commit eef504c

Browse files
authored
conditional statements
1 parent cb19bc2 commit eef504c

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

06 - Conditional statements

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
So from here the intresting part starts,
2+
3+
Here we are going to see what are conditional statements and how to use them in our program
4+
5+
what is a conditions?
6+
in our programing norms we call condition statements as yes or no question, which means
7+
the question we ask must result only , either True or False
8+
9+
in our previous blog we have seen that the relational statements result in either True or false , Hence we use
10+
relational in our conditional statements
11+
12+
So ok, Now let's what are conditional statements?
13+
In programming, a conditional statements is nothing but a statement which CHANGES THE FLOW OF THE CONTROL
14+
according to the contdition given
15+
16+
Got confused let's see an example ,
17+
consider you need to write a program for checking elegiblity for voting
18+
So, the condition here is age >=18
19+
if the age is greate than 18 then print you are elegible for voting
20+
if not then print you are not elegible for voting
21+
22+
SYNTAX:
23+
if (condition):
24+
statements
25+
else:
26+
statements
27+
28+
so let's write the code,
29+
age = int(input('enter your age: '))
30+
if age >= 18:
31+
print('you are elegiable for voting')
32+
else:
33+
print('you are not elegible for voting')
34+
35+
so let's we what we have done
36+
in the first line we are getting an input from user and converting to integer as input() returns string datetype and we are storing the integer value in the variable age
37+
in the second line we are checking for the condition ,
38+
if (age >= 18) then the if block gets executed , ok what is a if block
39+
40+
a block is noting but a group of statements which has same number of spaces or tabs, Normally we use {} to represent a block but in python
41+
we use spaces or tabs to represent a block , and the spacing is known as indendation
42+
43+
in the program we have indented the print statement and it is below the if statements ,hence it is called if-block
44+
and the print statements below the else is called else block
45+
46+
Now comming 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)
47+
if the condition returns False , then the else block get's executed(i.e the program will print you are not elegible for voting)
48+
49+
hence, here we have got two print statements, which get's executed according to the condition, and this is the power of print statements
50+
51+
But what is we need to chech more than one condition, let's consider an example you are going to a roller coster ride ,
52+
but they are saying that, you need to be above 150 cm or weight less than 70kgs , so here we have two conditions, right?
53+
54+
so here comes the if-elif-else structure
55+
56+
SYNTAX:
57+
if (condition 1):
58+
statements
59+
elif (condition 2):
60+
statements
61+
.
62+
.
63+
.
64+
else:
65+
statements
66+
67+
68+
as you can see we have condition 1, condition 2, .... and at last we have else which gets executed if none of the above conditions get's stisfies
69+
70+
let's write a program
71+
72+
height = int(input('enter your height in cm: '))
73+
witght = int(input('enter your weight in kgs: '))
74+
if(height >= 150):
75+
print('you may ride!')
76+
elif (weight <= 70):
77+
print('you may ride!')
78+
else:
79+
print('sorry!, you are not elegiable')
80+
81+
here we have two condition one is if the height is greater than 150 then it prints you may ride! as output
82+
the sencond condition get's executed only if the above condition is not satisfied and the corrent conndition is satisfied
83+
(i.e if i give height as 140 and weight as 60 the if-block is skipped and the elif-block gets get executed )
84+
85+
if I give the height as 140 and weight as 80 then the both condition as skipped and the last statements , else get's
86+
executed and prints sorry!, you are not elegiable
87+
88+
as a conclusion , the conditional statemenst are used the execute as certain block accoring to the condition given
89+
90+
91+
92+
93+
94+
95+
96+
97+

0 commit comments

Comments
 (0)