In this lesson you’ll see practical examples of the two loop interruption options you’ve learned about in the last lesson. The used examples are debugged in the video, so that you get a sense of what’s happening behind the scenes. The examples covered in this lesson are displayed below.
n = 5 while n > 0: n = n - 1 if n == 2: break print(n) print("Loop is finished")
n = 5 while n > 0: n = n - 1 if n == 2: continue print(n) print("Loop is finished")
If you’d like to explore another use case for the
break statement, then check out How Can You Emulate Do-While Loops in Python? from Real Python.