if, else, and elif statements
if
else
elif
Control flow refers to the order in which individual statements, instructions, or function calls are executed or evaluated.
The if statement is used to conditionally execute a block of code.
if condition: statement
x = 10 if x > 5: print("x is greater than 5")
x = 10 y = 3 if x > 5 and y < 7: print("x is greater than 5")
The else statement is used to execute a block of code if the condition in the if statement is false.
if condition: statement else: statement
x = 10 if x > 15: print("x is greater than 15") else: print("x is not greater than 15")
The elif (short for else if) statement is used to check multiple expressions for true and execute a block of code as soon as one of the conditions is true.
if condition1: statement elif condition2: statement else: statement
x = 10 if x > 15: print("x is greater than 15") elif x > 5: print("x is greater than 5 but not greater than 15") else: print("x is not greater than 5")