1 of 17

Control Flow: Loops (con’t) and Nested Loops

Winter 2025

1

Adrian Salguero

2 of 17

Announcements

  • Homework 1 is now available, due Friday January 17th at 11:59pm
  • Coding Practice 1 due Friday January 17th at 11:59pm
  • No lecture next Monday, January 20th
    • Martin Luther King Jr. Day
  • Section AC (Thursdays, 1:30pm-2:20pm) now in PAR 220

2

3 of 17

Some Loops

# Sum of a list of values, what values?

result = 0

for element in range(5):

result = result + element

print("The sum is: " + str(result))

# Sum of a list of values, what values?

result = 0

for element in range(5, 1, -1):

result = result + element

print("The sum is:", result)

# Sum of a list of values, what values?

result = 0

for element in range(0, 8, 2):

result = result + element

print("The sum is:", result)

# Sum of a list of values, what values?

result = 0

size = 5

for element in range(size):

result = result + element

print("When size = " + str(size) + " result is " + str(result))

3

4 of 17

How to process a list:�One element at a time

  • A common pattern when processing a list:

result = initial_value

for element in list:

result = updated result

use result

  • initial_value is a correct result for an empty list
  • As each element is processed, result is a correct result for a prefix of the list
  • When all elements have been processed, result is a correct result for the whole list

4

# Sum of a list

result = 0

for element in mylist:

result = result + element

print(result)

5 of 17

Examples of list processing

  • Product of a list:

result = 1

for element in mylist:

result = result * element

  • Maximum of a list:

curr_max = mylist[0]

for element in mylist:

curr_max = max(curr_max, element)

  • Approximate the value 3 by 1 + 2/3 + 4/9 + 8/27 + 16/81 + … = (2/3)0 + (2/3)1 + (2/3)2 + (2/3)3 + … + (2/3)10

result = 0

for element in range(11):

result = result + (2.0/3.0)**element

5

result = initial_value

for element in list:

result = updated result

The first element of the list (counting from zero)

6 of 17

Nested Loops

for i in [1, 2, 3]:

print("Before j loop i is", i)

for j in [50, 100]:

print("j is", j)

What is the output?

6

7 of 17

More Nested Loops

How many statements does this loop contain?

for i in [0, 1]:

print("Outer", i)

for j in [2, 3]:

print(" Inner", j)

print(" Sum", i + j)

print("Outer", i)

What is the output?

7

8 of 17

More Nested Loops

How many statements does this loop contain?

for i in [0, 1]:

print("Outer", i)

for j in [2, 3]:

print(" Inner", j)

print(" Sum", i + j)

print("Outer", i)

What is the output?

8

Output:

Outer 0

Inner 2

Sum 2

Inner 3

Sum 3

Outer 0

Outer 1

Inner 2

Sum 3

Inner 3

Sum 4

Outer 1

loop body:�3 statements

“nested”�loop body:�2 statements

9 of 17

Understand loops through the transformation approach

Key idea:

    • Assign each sequence element to the loop variable
    • Duplicate the body

9

for i in [0, 1]:

print("Outer", i)

for j in [2, 3]:

print(" Inner", j)

i = 0

print("Outer", i)

for j in [2, 3]:

print(" Inner", j)

i = 1

print("Outer", i)

for j in [2, 3]:

print(" Inner", j)

i = 0

print("Outer", i)

j = 2

print(" Inner", j)

j = 3

print(" Inner", j)

i = 1

print("Outer", i)

j = 2

print(" Inner", j)

j = 3

print(" Inner", j)

10 of 17

Test your understanding of loops

Puzzle 1:

for i in [0, 1]:

print(i)

print(i)

Puzzle 2:

i = 5

for i in []:

print(i)

Puzzle 3:

for i in [0, 1]:

print("Outer", i)

for i in [2, 3]:

print(" Inner", i)

print("Outer", i)

10

inner�loop�body

outer�loop�body

Output:

11 of 17

Test your understanding of loops

Puzzle 1:

for i in [0, 1]:

print(i)

print(i)

Puzzle 2:

i = 5

for i in []:

print(i)

Puzzle 3:

for i in [0, 1]:

print("Outer", i)

for i in [2, 3]:

print(" Inner", i)

print("Outer", i)

11

Outer 0

Inner 2

Inner 3

Outer 3

Outer 1

Inner 2

Inner 3

Outer 3

0

1

1

Output:

(no output)

inner�loop�body

outer�loop�body

Reusing loop variable�(don’t do this!)

12 of 17

Some More Loops

for size in [1, 2, 3, 4]:

print("size is", size)

for element in range(size):

print("element is", element)

12

13 of 17

Even More Loops

for size in [1, 2, 3, 4]:

result = 0

for element in range(size):

result = result + element

print("size=" + str(size) + " result=" + str(result))

print("We are done!")

print("result is", result)

13

What happens if we move result = 0

to be the first line of the program instead?

14 of 17

Fix this loop

# Goal: print 1, 2, 3, …, 48, 49, 50

for tens_digit in [0, 1, 2, 3, 4]:

for ones_digit in [1, 2, 3, 4, 5, 6, 7, 8, 9]:

print(tens_digit * 10 + ones_digit)

Questions:

  1. What does it actually print?
  2. How can we change it to correct its output?

Hint: Watch out for "edge conditions" (beginning or end of loop)

14

15 of 17

Some Fixes

for tens_digit in [0, 1, 2, 3, 4]:

for ones_digit in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:

print(tens_digit * 10 + ones_digit + 1)

for tens_digit in [0, 1, 2, 3, 4]:

for ones_digit in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:

print(tens_digit * 10 + ones_digit)

for ones_digit in [1, 2, 3, 4, 5, 6, 7, 8, 9]:

print(ones_digit)

for tens_digit in [1, 2, 3, 4]:

for ones_digit in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:

print(tens_digit * 10 + ones_digit)

print(50)

15

16 of 17

Loops over Strings

for letter in "hello":

print(letter)

my_string = "CSE 160"

for letter in my_string:

print(letter)

count = 0

for letter in my_string:

count = count + 1

print(count)

16

17 of 17

Todos for next lecture

  • Continue working on/submit Homework 1 and Coding Practice 1
  • Post any questions you have on Ed or come to office hours

17