Page 433 - CITS - Computer Software Application -TT
P. 433
COMPUTER SOFTWARE APPLICATION - CITS
# First if statement
if (i < 15):
print(“i is smaller than 15”)
# Nested - if statement
# Will only be executed if statement above
# it is true
if (i < 12):
print(“i is smaller than 12 too”)
else:
print(“i is greater than 15”)
Output:
i is smaller than 15
i is smaller than 12 too
if-elif-else Ladder
Here, a user can decide among multiple options. The if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and
the rest of the ladder is bypassed. If none of the conditions is true, then the final “else” statement will be executed.
Syntax:
if (condition):
statement
elif (condition):
statement
else:
statement
Example:
In the example, we are showing single if condition, multiple elif conditions, and single else condition.
# Python program to illustrate if-elif-else ladder
#!/usr/bin/python
i = 20
if (i == 10):
print(“i is 10”)
elif (i == 15):
print(“i is 15”)
elif (i == 20):
print(“i is 20”)
else:
print(“i is not present”)
Output:
i is 20
420
CITS : IT&ITES - Computer Software Application - Lesson 120 - 137