How to Exit Loops Early With the Python Break Keyword
In Python, the break
statement lets you exit a loop prematurely, transferring control to the code that follows the loop. This tutorial guides you through using break
in both for
and while
loops. You’ll also briefly explore the continue
keyword, which complements break
by skipping the current loop iteration.
By the end of this tutorial, you’ll understand that:
- A
break
in Python is a keyword that lets you exit a loop immediately, stopping further iterations. - Using
break
outside of loops doesn’t make sense because it’s specifically designed to exit loops early. - The
break
doesn’t exit all loops, only the innermost loop that contains it.
To explore the use of break
in Python, you’ll determine if a student