#PYTHON(DAY14)
Continuation of conditional statements
if-elif-else
- primary block will be ‘if’, this will evaluate the c.e and if it is true,
- it will execute code-block under ‘if’ statement. if the c.e turns out to false, then it will go to first ‘elif’ block
- ‘elif’ c.e is validated and if it is True, the code-block under this ‘elif’ will be executed, if not, it will go to next elif block and so on till it validates all the elif blocks.
- if none of the ‘if’, ‘else’ c.e if turns to be True, then else block will be executed
flow chart of if-elif-else:
Example
z = int(input('enter marks:'))
if z>80:
print('first class with distinction')
elif z>60 and z<80:
print('first class')
elif z>35 and z<60:
print('second class')
elif z<35:
print('fail')
if z>80:
print('first class with distinction')
elif z>60 and z<80:
print('first class')
elif z>35 and z<60:
print('second class')
elif z<35:
print('fail')
And the output is:
enter marks:46
second class
Nested if
- if inside the if
flow chart of Nested if:
Example
marks = int(input('enter marks:'))
c = time.time()
a = time.time()
if marks>=35:
if marks<60:
print('second class')
elif marks>60 and marks<80:
print('first class')
else:
print('fwd')
else:
print('fail')
d = time.time()
b = time.time()
c = time.time()
a = time.time()
if marks>=35:
if marks<60:
print('second class')
elif marks>60 and marks<80:
print('first class')
else:
print('fwd')
else:
print('fail')
d = time.time()
b = time.time()
And the output is:
enter marks:67
first class
No comments:
Post a Comment