Setting the Stage: Control Over Loops with Break and Continue

Hello, and welcome to this stimulating session! Today, you will delve into Python loops' governing principles with break and continue. These potent tools can halt a loop mid-way or bypass an iteration.

Sounds thrilling? Let's dive in!

Break: The Loop Controller in For Loops

The break keyword ends a loop before it exhausts all iterations:

The code prints:

In this for loop, once we reach Earth, break terminates the loop. We avoid unnecessary iterations over the remaining planets.

Break: The Loop Controller in While Loops

The break command works similarly in a while loop:

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal