0% found this document useful (0 votes)
15 views

Control Flow Part 1

Here is the code: salary = float(input("Enter your salary: ")) years = int(input("Enter your years of service: ")) bonus = 0 if years > 5: bonus = salary * 0.1 print(f"Your net bonus is: {bonus}")
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Control Flow Part 1

Here is the code: salary = float(input("Enter your salary: ")) years = int(input("Enter your years of service: ")) bonus = 0 if years > 5: bonus = salary * 0.1 print(f"Your net bonus is: {bonus}")
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Control Flow

(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

if a > 0 and b > 0:


print("The numbers are greater than 0")
Note:
If the first expression
evaluated to be false while
using and operator, then the
further expressions are not
evaluated.
# Python program to demonstrate logical or operator

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

dividend = float(input("Enter the dividend: "))


divisor = float(input("Enter the divisor: "))

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

dividend = float(input("Enter the dividend: "))


divisor = float(input("Enter the divisor: "))

if divisor > 0:
quotient = dividend / divisor
print(quotient)
Enter the dividend: 10
Enter the divisor: 2
5.0
# Simple if Statements

dividend = float(input("Enter the dividend: "))


divisor = float(input("Enter the divisor: "))

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'''

num = float(input("Enter a number: "))


if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
A company decided to give bonus of 10% to
employee if his/her year of service is more
than 5 years. Ask user for their salary and
year of service and print the net bonus
amount.

You might also like