Concept Flow - Why while loop is needed
Start
Check condition
Yes
Execute loop body
Repeat check condition
No
Exit
The while loop repeats a set of actions as long as a condition is true, stopping when it becomes false.
Jump into concepts and practice - no test required
count = 0 while count < 3: print(count) count += 1
| Step | count | Condition (count < 3) | Action | Output |
|---|---|---|---|---|
| 1 | 0 | True | Print 0, count = count + 1 | 0 |
| 2 | 1 | True | Print 1, count = count + 1 | 1 |
| 3 | 2 | True | Print 2, count = count + 1 | 2 |
| 4 | 3 | False | Exit loop |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| count | 0 | 1 | 2 | 3 | 3 |
while loop syntax:
while condition:
code to repeat
Repeats code as long as condition is True.
Stops when condition is False.
Useful for repeating unknown times until a condition changes.while loop in Python instead of a for loop?while and for loopsfor loops run a fixed number of times, while while loops run as long as a condition is true.while loops are neededwhile loops are useful when the number of repetitions is unknown before starting.while loop [OK]while loop in Python?while loop syntaxcount = 3
while count > 0:
print(count)
count -= 1
print('Done')i = 1
while i < 5:
print(i)
i += 1print(i) is not indented properly.while loop for this?