Control Flow

Control Flow

if, else, and elif statements

Control Flow

Control flow refers to the order in which individual statements, instructions, or function calls are executed or evaluated.

Control Flow

if Statement

The if statement is used to conditionally execute a block of code.

Syntax

if condition:
    statement
Control Flow

if 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")
Control Flow

else Statement

The else statement is used to execute a block of code if the condition in the if statement is false.

Syntax

if condition:
    statement
else:
    statement
Control Flow

else Statement

Example

x = 10
if x > 15:
    print("x is greater than 15")
else:
    print("x is not greater than 15")
Control Flow

elif Statement

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.

Syntax

if condition1:
    statement
elif condition2:
    statement
else:
    statement
Control Flow

elif Statement

Example

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")
Control Flow

Exercise: A More Advanced Calculator

http://gg.gg/1b80sx