This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 4: for loops

continue the action

face Josiah Wang

You probably saw this one coming - let’s have an audition to see whether you know how to continue, break and pass! 🎤

Again, it might be easier to think more semantically in terms of “what is the program trying to do?” rather than trying to ‘trace’ the output at each step.

1 2 3 4 5 6

Question 1

What is the output of the following code snippet? Type Error if it results in an error. Type Infinite if it ends up in an infinite loop.

1
2
3
4
5
6
7
8
9
numbers = [5, 3, 2, 1, 4, 6] 
total = 0
for number in numbers:
    if number % 2 == 0:
        continue

    total += number

print(total)
9
Explanation:

This piece of code adds up all odd numbers in a list. Lines 4-5 will skip any even numbers and continue with the next number.

Question 2

What is the output of the following code snippet? Type Error if it results in an error. Type Infinite if it ends up in an infinite loop.

1
2
3
4
5
6
7
8
9
numbers = [5, 3, 2, 1, 4, 6] 
total = 0
for number in numbers:
    if number % 2 == 0:
        break

    total += number

print(total)
8
Explanation:

This piece of code adds up all odd numbers that comes before the first even number in a list. Lines 4-5 will break out of the loop the moment it hits the first even number in the list.

Question 3

What is the output of the following code snippet? Type Error if it results in an error. Type Infinite if it ends up in an infinite loop.

1
2
3
4
5
6
7
8
9
numbers = [5, 3, 2, 1, 4, 6] 
total = 0
for number in numbers:
    if number % 2 == 0:
        pass

    total += number

print(total)
21
Explanation:

This piece of code adds up all numbers in the list. Lines 4-5 are redundant - they might as well not be there!

Question 4

What is the output of the following code snippet? Type Error if it results in an error. Type Infinite if it ends up in an infinite loop.

1
2
3
4
5
6
7
8
9
numbers = [2, 4, -1, 1, -3, 3] 
total = 0
for number in numbers:
    if number <= 0:
        continue

    total += number

print(total)
10
Explanation:

This piece of code adds up all positive numbers in a list. Lines 4-5 will skip any non-positive numbers and continue with the next number.

Question 5

What is the output of the following code snippet? Type Error if it results in an error. Type Infinite if it ends up in an infinite loop.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
matrix = [[1, 0, 2], [3, 1, -4], [2, -2, 2]] 
for row in matrix:
    row_total = 0
    for cell in row:
        row_total += cell

    if row_total <= 0:
        continue

    print(row_total)

3 2
Explanation:

This piece of code sums the numbers in each row of the matrix and prints out the sum only if the sum is positive. Also a note that lines 3-5 can potentially be refactored into its own function (remember that nested loops might be a sign that your code can be refactored).

Question 6

What is the output of the following code snippet? Type Error if it results in an error. Type Infinite if it ends up in an infinite loop.

1
2
3
4
5
6
7
8
9
matrix = [[1, 0, 2], [3, 1, -4], [2, -2, 2]] 
for row in matrix:
    row_total = 0
    for cell in row:
        if cell <= 0:
            continue
        row_total += cell

    print(row_total)

3 4 4
Explanation:

Like break, continue redirects to the beginning of the current innermost loop. In this case, continue will cause the program to be go to Line 4 and process the next cell in row. This piece of code sum the positive numbers in each row of the matrix and prints out the sum.

Also note that lines 3-7 can potentially be refactored into its own function, perhaps something like row_total = sum_positive(row).