if, elif, and else statements:
if Statement:The if statement is used to conditionally execute a block of code based on a given condition.
if condition:
# Code to be executed if the condition is True
elif Statement:The elif (else if) statement allows you to check multiple conditions sequentially after the initial if statement. It is executed only if the previous conditions are False.
if condition1:
# Code to be executed if condition1 is True
elif condition2:
# Code to be executed if condition2 is True
elif condition3:
# Code to be executed if condition3 is True
else:
# Code to be executed if none of the conditions are True
else Statement:The else statement is used to provide a default block of code that will be executed when none of the preceding conditions in an if-elif chain are True.
if condition:
# Code to be executed if condition is True
else:
# Code to be executed if condition is False
x = 10
if x > 10:
print("x is greater than 10")
elif x < 10:
print("x is less than 10")
else:
print("x is equal to 10")
In this example, the program checks whether x is greater than 10, less than 10, or equal to 10, and executes the corresponding block of code based on the condition.
Remember:
elif statements, but only one else statement (optional).This control flow structure allows you to create flexible and responsive programs by directing the execution path based on different conditions.