Chapter 5: Breaking the flow

Test your understanding

face Josiah Wang

Test out your understanding of break!

1 2 3 4 5 6 7

Question 1

What is output of the following code snippet? Write multiple outputs on separate lines. Write Error if you think the code will cause en arror. Write Infinite if you think the code with run forever without stopping.

1
2
3
4
5
6
7
i = 0
while i < 5:
    print(i)
    if i == 2:
        break
    i = i + 1
print("Out")
0 1 2 Out

Question 2

What is output of the following code snippet? Write multiple outputs on separate lines. Write Error if you think the code will cause en arror. Write Infinite if you think the code with run forever without stopping.

1
2
3
4
5
6
7
i = 0
while i < 5:
    if i == 2:
        break
    print(i)
    i = i + 1
print("Out")
0 1 Out
Explanation:

2 does not get printed in this case since the break statement is executed before the value can be printed.

Question 3

What is output of the following code snippet? Write multiple outputs on separate lines. Write Error if you think the code will cause en arror. Write Infinite if you think the code with run forever without stopping.

1
2
3
4
5
6
7
8
i = 0
while i < 5:
    if i == 2:
        break
        print("In")
    print(i)
    i = i + 1
print("Out")
0 1 Out
Explanation:

The statement print("In") is never executed because the program breaks out of the loop before that!

Question 4

What is output of the following code snippet? Write multiple outputs on separate lines. Write Error if you think the code will cause en arror. Write Infinite if you think the code with run forever without stopping.

1
2
3
4
5
6
7
8
i = 0
while i < 5:
    if i == 2:
        print("In")
        break
    print(i)
    i = i + 1
print("Out")
0 1 In Out
Explanation:

'In' in printed before breaking out of the loop.

Question 5

What is output of the following code snippet? Write multiple outputs on separate lines. Write Error if you think the code will cause en arror. Write Infinite if you think the code with run forever without stopping.

1
2
3
4
5
6
7
8
i = 0
while i < 5:
    if i < 0:
        print("In")
        break
    print(i)
    i = i + 1
print("Out")
0 1 2 3 4 Out
Explanation:

The body of the if statement is never executed.

Question 6

What is output of the following code snippet? Write multiple outputs on separate lines. Write Error if you think the code will cause en arror. Write Infinite if you think the code with run forever without stopping.

1
2
3
4
5
6
7
8
i = 0
while not False:
    if i < 0:
        print("In")
        break
    print(i)
    i = i + 1
print("Out")
Infinite
Explanation:

The break statement is never reached! And while not False (or while True) executes forever!

Question 7

What is output of the following code snippet? Write multiple outputs on separate lines. Write Error if you think the code will cause en arror. Write Infinite if you think the code with run forever without stopping.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
x = 0
while x < 3:
    i = 1
    while i < 5:
        if i == 3:
            break
        print(i)
        i = i + 1

    print("In")
    x = x + 1

print("Out")
1 2 In 1 2 In 1 2 In Out
Explanation:

Remember that a break statement forces the program out from its inner loop. This means that it still stays in the first loop (Line 10) after break-ing out.