Control Flow Part 1
Control Flow Part 1
(Conditional Statement)
Outline
1. Operators (review)
2. Control Flow
I. Sequential Statements
II. Conditional Statements
• if
• if-else
• if-elif-else
• Nested if and if-else statement
Example:
a = 9
b = 5
# Output
print(a > b)
# Python program to demonstrate logical
’and’ operator
a = 10
b = 10
a = 10
b = -10
c = 0
if a > 0 or b > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
if b > 0 or c > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
Note:
If the first expression
evaluated to be True while
using or operator, then the
further expressions are not
evaluated.
or
# Python program to demonstrate
# logical not operator
x = 2
y = 5
#output = False
print(x > y)
#output = True
not x > y
•
•
•
#This is a Sequential Statement
a = 20
b = 10
c = a + b
print(f'the sum is: {c}')
#This is a Sequential Statement
x = 50
y = 10
z = 9
total = x - y - z
print(f'the total is: {total}')
Allows a program to test conditions,
and execute instructions
only when the condition is True.
•
•
•
•
•
# Simple if Statements
if divisor > 0:
quotient = dividend / divisor
Block of codes
print(quotient)
Indentation
a = 10
b = -10
c = 0
if a > 0 or b > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
if b > 0 or c > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
•
•
age = int(input("Enter age: "))
Condition
if age < 18:
print("You are still a Minor.")
# Simple if Statements
if divisor > 0:
quotient = dividend / divisor
print(quotient)
Enter the dividend: 10
Enter the divisor: 2
5.0
# Simple if Statements
if divisor > 0:
quotient = dividend / divisor
print(quotient)
Enter the dividend: 10
Enter the divisor: 0
In the test expression, you can also
enclose them with parenthesis to make
your code more readable as this:
•
•
name = input('Enter your name: ')
password = input('Password: ')
if name == 'Mary':
print('Hello Mary')
if password == 'swordfish':
print('Access granted.')
else:
print('Wrong password.')
print('Thank you')
# if-else Statement
humidity = float(input("Enter humidity: "))
if 60 <= humidity <= 70:
print("Relative Humidity is optimal.")
else:
print("Adjust ventilation...")
Enter humidity: 67
Relative Humidity is optimal.
# if-else Statement
humidity = float(input("Enter humidity: "))
if 60 <= humidity <= 70:
print("Relative Humidity is optimal.")
else:
print("Adjust ventilation...")
Enter humidity: 59
Adjust ventilation...
•
•
age = int(input("Enter age: "))
Condition
if age < 18:
print("You are still a Minor.")
elif age >= 60: Another condition
print("You are a Senior Citizen.")
else:
print("You are an Adult.")
# if-elif-else Statement
water_level = float(input("Enter water level: "))
if 0 < water_level <= 1:
print("Water level is dangerously low.")
elif 1 < water_level <= 3:
print("Water level is low.")
elif 3 < water_level <= 5:
print("Water level is OK.")
elif 5 < water_level <= 8:
print("Water level is high")
else:
print("Water level is dangerously high.")
•
•
'''In this program, we input a number
check if the number is positive or
negative or zero and display
an appropriate message
This time we use nested if statement'''