Break the Loop (When You Must)
Learn how to exit or skip within loops.
We'll cover the following
Sometimes, we don’t want to run the entire code—just part of it—until a certain condition is met, like a user typing “quit” or a robot reaching a goal.
This is where Python gives us control tools like if
statements to check conditions, and break
to stop a loop immediately when needed.
It’s like saying: “Keep going… until this happens—then stop!”
Stopping a loop when a condition is met
In the example, we use a while True
loop to keep asking the user for input until they type "exit."
Then, we use a break
to stop the loop.
Get hands-on with 1400+ tech skills courses.