Concept Flow - Why loops are needed
Start
Need to repeat task?
No→End
Yes
Perform task once
More repetitions needed?
Yes→Perform task again
No
End
This flow shows how loops help repeat a task multiple times until no more repetitions are needed.
int count = 1; while (count <= 3) { Console.WriteLine("Hello " + count); count++; }
| Step | count | Condition (count <= 3) | Action | Output |
|---|---|---|---|---|
| 1 | 1 | True | Print 'Hello 1', count++ to 2 | Hello 1 |
| 2 | 2 | True | Print 'Hello 2', count++ to 3 | Hello 2 |
| 3 | 3 | True | Print 'Hello 3', count++ to 4 | Hello 3 |
| 4 | 4 | False | Exit loop |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| count | 1 | 2 | 3 | 4 | 4 |
Loops repeat tasks until a condition is false.
Syntax example: while(condition) { task; update; }
Without loops, repeating tasks needs manual code duplication.
Loops save time and reduce errors.
Always update variables to avoid infinite loops.for loop in C#?int sum = 0;
for (int i = 1; i <= 3; i++)
{
sum += i;
}
Console.WriteLine(sum);int i = 0;
while (i < 3)
{
Console.WriteLine(i);
}Pass the test and this pattern moves unknown