0% found this document useful (0 votes)
7 views22 pages

Loops

The document provides an overview of loops in programming, detailing types such as 'while' and 'for' loops, along with their structures and examples. It explains concepts like nested loops, break, continue, and pass statements, and compares the functionality of 'for' and 'while' loops. Additionally, it includes practice exercises and homework assignments related to the topic.

Uploaded by

Perpetual Nkata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views22 pages

Loops

The document provides an overview of loops in programming, detailing types such as 'while' and 'for' loops, along with their structures and examples. It explains concepts like nested loops, break, continue, and pass statements, and compares the functionality of 'for' and 'while' loops. Additionally, it includes practice exercises and homework assignments related to the topic.

Uploaded by

Perpetual Nkata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

3.

LOOPS

Summer 2025
REVIEW: if, if-else, if-elif-else,
nested if
 if: for exceptional

 if-else: for two choices.

 If-elif-….-else: for many choices

 Nested if: there are conditional choices inside of any


condition
Loop = REPETITIONS

Types of Loops:

 for
 while
1. while Loop:
while (Boolean expression):
statements
(1). Test the Boolean expression.

(2). If True:
then execute statements in
loop body, then go back to step 1).

(3). If False, then continue on to


statement right after the loop.
Example 1: Print 10 times of “Hello
Python”
# To avoid infinite loop, you need to:
# (0) set up an initial value count = 0
# (1) set the limit for the count
# (2) increment the count

# print(count)

count = 0
while (count < 9):
print ('The count is:', count, “Hello Python”)
count += 1

print( "Good bye!")


Different way using while loop:
# use while True and conditional break

count = 0
while True:
if count > 10:
break
count += 1
print("Hello Python")
Stop loop by user’s input
# another example: for play game
while True:
play_again = input(" do you want to play again, y for yes and n for no>>")

if (play_again == "n“) or (play_again == "N“):


break

print(" playing game!\n")

print("bye")
2. for Loop: through a sequence
(1). Test the sequence if it is the end of it

(2). If False:
then execute statements in loop body,
then go to step (2).

(3). If True, then continue on to the statement


right after the loop.
Example: print 10 times “Hello
Python”
# use a sequence
for num in range(10): # [0, 10, 1)
print(num, "Hello Python")

#--------------------------------------------------
# (1) count is initialized as 0,
# the first element of sequence of 0, 1, 2, …, 9
# (2) then check if count < = 9 or not
# (3) if yes, print out
Range
range(start,stop,step)
• start: integer ( starting 0 by default)
• stop: integer (exclusive) i.e., end at stop – 1.
• step: integer (increment) 1 is default value

 Compare with slicing


list_a[start:stop:step]
? Question:
# loop: check condition first or run once first?

#what is the output


for count in range(1):
print(“loop!")
Nested Loops
 Loop inside of loop

 Similar to the if inside of if


Example
# outer loop
for i in range(3): # 0, 1, 2
print("OUTER", i )

# inner loop
for j in range(4): # 0, 1, 2, 3
print(" inner ", j)

print("\n")
Example: 9x9 table
# example (9x9 table)

# outer loop
for i in range(1,10):

# inner loop
for j in range(1, 10):
print(i*j, end =" ")

print()
Break/Continue: loop interruption
 for loop and while loop

 Break:
◦ using if condition:
◦ True, then break the loop

 Continue:
◦ Also using if condition:
◦ True, then skip the rest of loop
◦ Next round
Example 1: break
# your are given integer 1-100, print the first 5 even numbers

count = 0 # count how many

for i in range(1, 101):


if i%2 == 0:
print(i)
count +=1

if count >= 5:
break
Example 2: continue
# example for continue: print number 1-10 but not 5

for number in range(1, 11):


if number == 5:
continue # go to next loop,
# skip the rest of the in the loop

print('Number is ' + str(number))

print('Out of loop')
Pass: do not change the loop even if the
condition is true
# Using pass, the program runs exactly, as it would if there were no
conditional statement in the program.

# The pass statement tells the program to disregard that condition and
continue to run the program as usual.

# for test purpose

for number in range(10):


if number == 5:
pass # pass here

print('Number is ' , number)

print('Out of loop')
Comparison: while loop and for loop
A for loop:
◦will “do” something to everything

A while loop:
◦ will “do” something until a condition is
met.
 Exchangeable
Practice: Fizz-buzz
 Run a loop to check a number between 1-20
◦ if a number is divisible by 3 print Fizz,
◦ by 5, print Buzz,
◦ If both divisible, print Fizz Buzz,
◦ any other number, print the number itself

Divisible by 3: num%3 == 0
HW 1
 H1.A Letter Grades
 H1.B May Calendar
 END

You might also like