while
Loops as long as the condition is true.
Syntax
while condition:
...Description
Used when you don't know up front how many repetitions there'll be. Don't forget to make the condition false at some point, or it'll be an infinite loop. You exit with break.
Example
Loading editor…
Expected output
0 1 2
When to use?
Continuing until the user says 'quit', asking until correct input is given.
Common mistakes
- ⚠️ Forgetting the line that changes the condition causes an infinite loop.
- ⚠️ Add advancement like counter += 1.