Search⌘ K

Break the Loop (When You Must)

Explore how to control loop execution in Python by using break to exit loops when a condition is met and continue to skip iterations. This lesson teaches you to manage infinite loops and handle interactive input efficiently.

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 ...