forked from trekhleb/learn-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_break.py
25 lines (18 loc) · 798 Bytes
/
test_break.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""BREAK statement
@see: https://docs.python.org/3/tutorial/controlflow.html
The break statement, like in C, breaks out of the innermost enclosing "for" or "while" loop.
"""
def test_break_statement():
"""BREAK statement"""
# Let's terminate the loop in case if we've found the number we need in a range from 0 to 100.
number_to_be_found = 42
# This variable will record how many time we've entered the "for" loop.
number_of_iterations = 0
for number in range(100):
if number == number_to_be_found:
# Break here and don't continue the loop.
break
else:
number_of_iterations += 1
# We need to make sure that break statement has terminated the loop once it found the number.
assert number_of_iterations == 42