Python theorytheory 0/50 · 0%
Control flow · easy

11. while loops and control statements

Looping until a condition changes; break and continue.

`while` loops run as long as a condition holds. `break` exits the loop immediately; `continue` skips to the next iteration.

python
n = 10
steps = 0
while n != 1:
    n = n // 2 if n % 2 == 0 else 3 * n + 1
    steps += 1

A `for`/`while` can have an `else` clause that runs only if the loop completes without `break` — useful for search patterns:

python
for tx in txs:
    if tx.get("suspicious"):
        break
else:
    print("no suspicious tx found")

Check your understanding

  1. 1. What does `break` do inside a loop?

  2. 2. When does a `for...else` clause execute?

  3. 3. What does `continue` do?