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!
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.
The break
command works similarly in a while
loop:
