COGNITION FOUNDRYLEARN. BUILD. UNDERSTAND.
PY-010 / Repeat until something changes◷ 20 min read + practice

While loops and loop control

A for loop follows a set of values. A while loop keeps going for as long as its condition remains true. That flexibility is powerful — and requires careful thought about when the loop ends.

By the end, you can: Create a terminating while loop and explain the effects of break, continue and loop state.
Before this lesson: PY-009

A while loop checks its condition first

while count < 4: tests the condition before every iteration. When it becomes false, execution continues after the loop. If it starts false, the body does not run at all.

The body generally needs to change the state used by the condition. Without that change, a condition that starts true may stay true forever.

Design the exit before the loop

Before running a loop, write down its stopping condition. Then confirm which statement causes progress towards that condition. In a counter loop, increment count on every normal pass.

This matters especially when continue is involved: it jumps to the next check and skips any code that follows it in the loop body. Moving the increment below a continue can accidentally create a loop that never advances.

break and continue have distinct jobs

break leaves the nearest enclosing loop immediately. continue skips the rest of the current iteration and moves to the next one. Neither leaves the entire function; they affect only the loop.

Use break for a clear early exit, such as finding the first matching item. Use continue for a value you want to ignore, while making sure state still progresses correctly.

Choose the simplest loop that works

If you already have a collection of items, for item in items is usually easier to read than managing an index with while. Choose while when the number of repetitions depends on a condition that changes dynamically.

For demonstrations, use small fixed limits and known exit conditions. The browser playground can stop a runaway execution, but a timeout is a safety mechanism, not a substitute for correct loop design.

Try it yourself.

Choose a starting example or write something completely different. You control the code; the Run button executes what is in the editor, not a hidden preset.

CF Python PlaygroundBrowser-based Python
Ready to load Python
Countdown with while

Decrease a counter until the condition is false.

Syntax colours highlight Python code. Tab inserts four spaces; press Escape then Tab to leave the editor. Ctrl+Enter runs the current code.
OUTPUT / ERRORS
Press Run Python to execute the editable code.
NOW MAKE IT YOURS

Begin with remaining = 0. Why does the body not run?

Need a hint?

while checks the condition before its first iteration.

On first Run, a pinned Python runtime downloads from jsDelivr. Code executes locally in your browser's Web Worker, not on our server. It cannot inspect your normal PC files or Windows hardware. Avoid entering secrets. Heavy/infinite loops are stopped by a time limit.

Check your understanding.

Try answering each question before opening its reference answer. Explain the idea aloud or change an example to test it for yourself.

  1. Why might a while loop run forever even when it contains a continue statement?

    View answer

    continue skips all remaining statements in the current iteration. If a counter increment or state update sits after it, the loop may keep returning to the same unchanged condition and never reach its stopping point. Put the progress update before paths that continue, and reason about every branch.

  2. What is the difference between break and continue?

    View answer

    break leaves the nearest enclosing loop immediately, so execution resumes after that loop. continue does not exit the loop: it skips the rest of the current iteration and starts the next condition check or next item. Use them intentionally and keep the loop’s exit condition clear.

Where this leads.

Loops become more useful when we can hold several related values together in one collection.