Skip to content
IT Vizag
← Decisions & Loops

▶ Sign up free for the video, slides & prompts

The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.

Checking…

break and continue

Lesson 6 of 9 · 1 min read

Stop a loop early or skip a step.

🎬 Video version coming soon — read it now

Sometimes you need to leave a loop early, or skip one round. Two keywords do that.

  • break stops the loop completely
  • continue skips the rest of this round and moves to the next

break: stop when you've found it

🔒 Code example: sign up free (above) to unlock it.

Once the answer is found there's no need to check the other numbers.

The while True pattern

A very common way to write a menu or a "keep going until quit" program:

🔒 Code example: sign up free (above) to unlock it.

while True would loop forever on its own, so the break inside is the only way out.

continue: skip this one

Add only the positive numbers:

🔒 Code example: sign up free (above) to unlock it.

Use them sparingly

break and continue are useful, but loops full of them become hard to follow. If you have three or four, there is usually a clearer way to write the condition.

Tip: break only exits the innermost loop it is in. That matters with nested loops, which come next.

In one line

break leaves the loop now, continue skips to the next round, and while True with a break is the classic menu loop.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend