This lesson covers two different ways to interrupt a loop iteration. The two options consist of the usage of the two keywords break and continue. You’ll learn the differences between both approaches in this lesson.
Interrupting Loop Iteration
00:00
Okay. Let’s look at how we can actually break out of these loops prematurely. There’s two ways. We can use the break keyword or the continue keyword.
00:10
If we use break, we’re immediately going to terminate the loop entirely, so I’m actually going to just jump down to the last statement after the while loop. But if I use continue, I’ll actually jump back up to the while loop and check if the next condition or the next iteration still meets the condition and returns True, and if it does, then I will continue on with my loop.
00:34
So, the code actually looks pretty similar. We can exchange these words pretty evenly. If I use break or if I use continue, I can put them pretty much wherever I want, and we’ll see that the output is really what’s going to be different.
Become a Member to join the conversation.
