Lesson 4 Repeated Practice Makes Perfect Chapter 1: Introduction Chapter 2: Logical operators Chapter 3: While loops Chapter 4: Applied problem solving Chapter 5: Breaking the flow [5.1] Altering the flow of loops [5.2] break [5.3] Another use case for break [5.4] Test your understanding [5.5] Newton's method with break [5.6] Newton's method (solutions) Chapter 6: More applied problems Chapter 7: Enhancing your robot Chapter 8: Debugging your code Chapter 9: The PEP 8 style guide Chapter 10: Summary 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 7i = 0 while i < 5: print(i) if i == 2: break i = i + 1 print("Out") Output 0 1 2 Out Check answer! 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 7i = 0 while i < 5: if i == 2: break print(i) i = i + 1 print("Out") Output 0 1 Out Explanation: 2 does not get printed in this case since the break statement is executed before the value can be printed. Check answer! 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 8i = 0 while i < 5: if i == 2: break print("In") print(i) i = i + 1 print("Out") Output 0 1 Out Explanation: The statement print("In") is never executed because the program breaks out of the loop before that! Check answer! 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 8i = 0 while i < 5: if i == 2: print("In") break print(i) i = i + 1 print("Out") Output 0 1 In Out Explanation: 'In' in printed before breaking out of the loop. Check answer! 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 8i = 0 while i < 5: if i < 0: print("In") break print(i) i = i + 1 print("Out") Output 0 1 2 3 4 Out Explanation: The body of the if statement is never executed. Check answer! 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 8i = 0 while not False: if i < 0: print("In") break print(i) i = i + 1 print("Out") Output Infinite Explanation: The break statement is never reached! And while not False (or while True) executes forever! Check answer! 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 13x = 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") Output 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. Check answer!