Overview
A while loop repeats its body as long as a condition stays True.
Analogy
Waiting for a bus: keep checking, board when it arrives.
Step-by-step
- Write condition: while n > 0:
- Update the variable inside the loop to eventually make it False.
- break to exit early; continue to skip the rest of the body.
- Infinite loop: while True: ... break when done.
Visual
n=3 → True(body,n=2) → True(body,n=1) → True(body,n=0) → False → done
Common mistakes
- Forgetting to update the loop variable causing an infinite loop.
- Using == with floats as loop condition (float precision).
Practice questions
- Implement a countdown from 10 to 1.
- Read user input until 'quit' is entered.