Concept Flow - Why loops are needed
Start
Need to repeat tasks?
Yes/No
Do once
Repeat task
End or Repeat
This flow shows deciding if a task needs repeating. If yes, use a loop to repeat it; if no, do it once.
<?php for ($i = 1; $i <= 3; $i++) { echo "Hello $i\n"; } ?>
| Iteration | Variable i | Condition i <= 3 | Action | Output |
|---|---|---|---|---|
| 1 | 1 | True | Print 'Hello 1' | Hello 1 |
| 2 | 2 | True | Print 'Hello 2' | Hello 2 |
| 3 | 3 | True | Print 'Hello 3' | Hello 3 |
| 4 | 4 | False | Exit loop |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| i | 1 | 2 | 3 | 4 | 4 |
Loops repeat tasks automatically.
Syntax: for (init; condition; update) { code }
Loop runs while condition is true.
Stops when condition is false.
Saves writing repeated code.