Decision making is required when we want to execute a code if a specific condition is satisfied. If else is used for Decision Making
Sample Example on If else:
Syntax:
if test expression:
Code to be executed if condition in True .
else:
Code to be executed if condition in False.
Here code will only be executed if the condition provided for IF statement is True.
Else it will Run the code under the Else Statement
Sample Example on If else:
a = 10
if a == 10:
print("The Number is 10")
else:
print("The Number is not 10")
if else and elif flow chart:
If elif else:
elif if is short cut for else if. If the the first if condition is False it goes on to check next elif condition and so on. If all the conditions are false it will enter into else and execute the code inside else.
Syntax:
if test expression 1:
Code to be executed if condition1 in True .
elif test expression 2:
Code to be executed if condition2 in True .
else :
Code to be executed if condition1 and condition2 are False.
Sample example for if else elif:
a = 10
if a == 10:
print("Number is 10")
elif a <= 10:
print("Number is less than 10")
Comments
Post a Comment