Sometimes the programmer needs to check the evaluation of certain expression(s), whether the expression(s) evaluate to True or False. If the expression evaluates to False, then the program execution follows a different path than it would have if the expression had evaluated to True.
Based on this, the conditional statements are further classified into following types:
Execute the block of code inside if statement. After execution return to the code out of the if……else block.\
Execute the block of code inside else statement. After execution return to the code out of the if……else block.
applePrice = 210
budget = 200
if (applePrice <= budget):
print("Alexa, add 1 kg Apples to the cart.")
else:
print("Alexa, do not add Apples to the cart.")
Alexa, do not add Apples to the cart.
Sometimes, the programmer may want to evaluate more than one condition, this can be done using an elif statement.
Execute the block of code inside if statement if the initial expression evaluates to True. After execution return to the code out of the if block.
Execute the block of code inside the first elif statement if the expression inside it evaluates True. After execution return to the code out of the if block.
Execute the block of code inside the second elif statement if the expression inside it evaluates True. After execution return to the code out of the if block.
.
.
.
Execute the block of code inside the nth elif statement if the expression inside it evaluates True. After execution return to the code out of the if block.
Execute the block of code inside else statement if none of the expression evaluates to True. After execution return to the code out of the if block.
num = 0
if (num < 0):
print("Number is negative.")
elif (num == 0):
print("Number is Zero.")
else:
print("Number is positive.")
Number is Zero.
We can use if, if-else, elif statements inside other if statements as well.
Example:
num = 18
if (num < 0):
print("Number is negative.")
elif (num > 0):
if (num <= 10):
print("Number is between 1-10")
elif (num > 10 and num <= 20):
print("Number is between 11-20")
else:
print("Number is greater than 20")
else:
print("Number is zero")
Output:
Number is between 11-20
If we wanna use 2 conditions in if or elfi statement, we have yo use and:
time1 = 10
if(time1<5 and time1>12):
print("Good Moening Sir")
Good Moening Sir