Making Decisions and
Performing Loops
Unit 3
CC S 1 50 0
Objectives
❑ Identify when to use an indentation
❑ Differentiate if, if-else, if-elif-else, and nested
if
❑ Define the match statement
❑ Describe the while loop and for loop
❑ Differentiate break, continue, and else statements
❑ Create a program implementing selection and iteration
2
Making Decisions and
Performing Loops
3
❑ Running a block of code based on a particular decision
− decisions based on a condition
❑ Indentation
− used to declare a block
− if the statements are on the same level of indentation, then
they are on the same block
if, elif, and else
Statements
5
if Statement
− used to test a specific condition, if the condition is true, a
block of code (if-block) will be executed
− condition can be any valid logical expression
if condition: True
condition if block
statement(s)
False
x = 100
y = int(input("Enter a number: "))
if y > x:
print(y, " is larger than ", x)
if-else Statement
− if the condition in the if statement is false, it will execute the
else statement
if condition: true
statement(s) condition if block
else:
statement(s)
false
else block
x = int(input("Enter a number: "))
y = int(input("Enter another number: "))
if y > x:
print(y, " is larger than ", x)
else:
print(x, " is larger than ", y)
late = True
if late:
print('I need to call my manager!')
else:
print('no need to call my manager...')
elif Statement
− checks multiple conditions and executes the block of code of
the condition that evaluated to true
− optional like else
− can contain multiple elif after an if
if condition1:
statement(s)
elif condition2:
statement(s)
elif condition3:
statement(s)
else:
statement(s)
false
condition
true false
condition
if block
true false
condition
elif block
true
elif block else block
x = int(input("Enter a number: "))
y = int(input("Enter another number: "))
z = int(input("Enter another number: "))
if x>y and x>z:
print(x, " is the largest number")
elif y>x and y>z:
print(y, " is the largest number")
else:
print(z, " is the largest number")
income = 20000
if income < 15000:
tax_coefficient = 0.0
elif income < 30000:
tax_coefficient = 0.2
elif income < 100000:
tax_coefficient = 0.35
else:
tax_coefficient = 0.45
print('I will pay:', income * tax_coefficient, ' in taxes')
Nested if
− allows the use of another if statements inside an if
if condition:
statement(s)
if condition:
statement(s)
x = int(input("Enter a number: "))
if x > 0:
print(x, " is a positive number.")
if x > 100:
print("It is greater than 100.")
else:
print("The number is ", x)
match Statement
− used to perform different actions based on different
conditions
match expression:
case x:
statement(s)
case y:
statement(s)
case z:
statement(s)
17
for Loop
18
The for Loop
− used for iterating on a sequence (either a list, a tuple, a
dictionary, a set, or a string)
− a set of statements may be executed once for each item,
tuple, set, etc.
− does not require an indexing variable to set beforehand
fruits = ["orange", "grapes", "apple"]
for x in fruits: List
print(x)
for x in "Python":
String
print(x)
The break Statement
− can stop the loop before it has looped through all the items
fruits = ["orange", "grapes", "apple"]
for x in fruits:
print(x)
if x == "grapes":
break
fruits = ["orange", "grapes", "apple"]
for x in fruits:
if x == "grapes":
break
print(x)
The continue Statement
− can stop the current iteration of the loop, and continue with
the next
fruits = ["orange", "grapes", "apple"]
for x in fruits:
if x == "grapes":
continue
print(x)
The range() Function
− returns a sequence of numbers from 0 and increments by 1,
by default, and ends at a specified number
for x in range(10):
print(x)
for x in range(5, 10):
print(x)
for x in range(1, 20, 2):
print(x)
The else in for Loop
− block of statements to be executed when the loop is finished
for x in range(10):
print(x)
else:
print("Completed!")
− cannot be executed if the loop is stopped by a break
statement
for x in range(10):
if x == 7: break
print(x)
else:
print("Completed!")
Nested Loop
− a loop inside a loop
− the inner loop will be executed once per iteration of the outer
loop
desc = ["fresh", "sweet", "tasty"]
fruits = ["orange", "grapes", "apple"]
for x in desc:
for y in fruits:
print(x, y)
The pass Statement
− for loop cannot be empty
for x in [1, 20, 2]:
pass
odd_numbers = [1, 3, 5, 7, 9, 11, 13, 15]
sum = 0
for num in odd_numbers:
sum += num
print("Their sum is: ", sum)
while Loop
29
The while Loop
− as long as the condition is true, it can execute a block of
statements
− needs to define an indexing variable
• increment/decrement this variable or the loop will not end
while condition:
action while condition is true
i = 1
while i <= 10: True
condition action if true
print(i)
i += 1
False
The break Statement
− the loop can be stopped even if the while condition is still
true
i = 10
while i <= 10:
print(i)
if i == 1:
break
i -= 1
The continue Statement
− stop the current iteration and continue with the next
i = 0
while i < 10:
i += 1
if i == 7:
continue
print(i)
The else Statement
− can run a block of code when the condition is no longer true
i = 1
while i < 10:
print(i)
i += 1
else:
print(i, " is greater than 9")
The do-while Loop
− can run at least once
− Python does not have a built-in do-while loop
• but possible to emulate
secret_word = "python"
counter = 0
while True:
word = input("Enter the secret word: ").lower()
counter = counter + 1
if word == secret_word:
break
if word != secret_word and counter > 7:
print("The secret word is ", secret_word)
break
Usual Logical Conditions
❑ x == y
❑ x != y
❑x<y
❑x>y
❑ x <= y
❑ x >= y
Using the Logical and and
or Operators
❑ and and or
− used to combine conditional statements
❑ not
− used to reverse the result of the conditional statement
Truth Table for and and or
x y x and y x or y
T T T T
T F F T
F T F T
F F F F
x = 78
y = 30
z = 54
if x>y and x>z:
print(x, " is the largest number")
elif y>x and y>z:
print(y, " is the largest number“)
else:
print(z, " is the largest number")
x = 54
y = 150
if not x > y:
print(x, " is NOT greater than ", y)
x = 78
y = 30
z = 54
if x>y or z>y:
print(“At least one of the conditions is True")