Concept Flow - while True pattern
Start
while True
Execute body
Check for break?
Yes→Exit loop
No
while True
The loop runs forever until a break condition inside the loop stops it.
while True: user_input = input('Enter q to quit: ') if user_input == 'q': break print('You typed:', user_input)
| Step | user_input | Condition (user_input == 'q') | Action | Output |
|---|---|---|---|---|
| 1 | 'hello' | False | print('You typed: hello') | You typed: hello |
| 2 | 'world' | False | print('You typed: world') | You typed: world |
| 3 | 'q' | True | break loop | |
| Exit | Loop ends because user_input == 'q' |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| user_input | None | 'hello' | 'world' | 'q' | 'q' |
while True creates an infinite loop. Use break inside the loop to stop it. Check conditions inside the loop body. Without break, loop runs forever. Common for input loops or waiting for events.