Concept Flow - Why loops are needed
Start
Need to repeat tasks?
Yes/No
Do once
Repeat task
End
This flow shows that when we need to do a task many times, we use loops to repeat it easily instead of writing the same code again and again.
for i in range(3): print(f"Hello {i}")
| Iteration | i value | Condition | Action | Output |
|---|---|---|---|---|
| 1 | 0 | 0 < 3 is True | Print 'Hello 0' | Hello 0 |
| 2 | 1 | 1 < 3 is True | Print 'Hello 1' | Hello 1 |
| 3 | 2 | 2 < 3 is True | Print 'Hello 2' | Hello 2 |
| 4 | 3 | 3 < 3 is False | Exit loop |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| i | N/A | 0 | 1 | 2 | 3 |
Loops repeat tasks multiple times without rewriting code. Syntax example: for i in range(n): Runs code block n times with i from 0 to n-1. Stops when condition is false. Saves time and reduces errors.