0. The Real Goal of This Lesson
A
whileloop repeats based on a condition,
but what actually stops the loop is state change.
The tools that create state change are:
+=-=*=++--
Without them, loops do not evolve.
They freeze.
And frozen state inside a loop is dangerous.
1. Start With a Basic Review
int number = 0;
while (number < 5)
{
number = number + 1;
Console.WriteLine(number);
}
This prints:
1
2
3
4
5
Why?
Because the state (number) changes every iteration.
2. The += Operator (Compound Assignment)
This line:
number = number + 1;
Can be simplified to:
number += 1;
It means:
Take the current value of
number,
add 1,
and store the result back intonumber.
No logic change.
Only structural simplification.
3. Runnable Example — Increasing by 2
using System;
class Program
{
static void Main()
{
int number = 0;
while (number < 10)
{
number += 2;
Console.WriteLine(number);
}
Console.WriteLine("Finished.");
Console.ReadKey();
}
}
Output:
2
4
6
8
10
The loop progresses because the state moves forward.
4. += Also Works With Strings
using System;
class Program
{
static void Main()
{
string text = "ab";
text += "cd";
Console.WriteLine(text); // abcd
Console.ReadKey();
}
}
Strings can accumulate values.
This is still state change.
Different type.
Same principle.
5. Dangerous Code — Infinite Loop
Now the critical part.
Incorrect Example
using System;
class Program
{
static void Main()
{
int number = 0;
while (number < 10)
{
number *= 2;
Console.WriteLine(number);
}
}
}
What happens?
The console keeps printing:
0
0
0
0
...
The program never stops.
6. Why Did This Happen?
Initial state:
number = 0
Inside the loop:
number *= 2;
0 × 2 = 0
The state does not change.
Condition:
0 < 10 → always true
The loop never reaches a false condition.
This is an infinite loop.
7. Definition of an Infinite Loop
A loop whose condition never becomes false.
The usual causes:
- No state change
- Incorrect state change
- Condition that can never fail
Infinite loops are logic failures.
Not syntax failures.
8. Fixing the Problem
Change the initial value:
int number = 1;
Now the flow becomes:
1 → 2 → 4 → 8 → 16
When number becomes 16:
16 < 10 → false
The loop exits correctly.
Termination restored.
9. The ++ Operator (Increment Operator)
This:
number += 1;
Can be shortened to:
number++;
It means:
Increase
numberby 1.
Same state change.
Cleaner syntax.
10. Pre-Increment vs Post-Increment (Simplified for Now)
number++;
++number;
Inside a loop body:
There is no visible difference.
The difference only matters when used inside expressions.
At this stage, treat them as equivalent for loop progression.
11. The Structural Model of a Loop
Every loop requires three components:
| Component | Purpose |
|---|---|
| Condition | Decides whether to continue |
| State variable | Value used inside condition |
| State change | Mechanism that eventually makes the condition false |
If one is missing:
- Infinite loop
- Immediate termination
- Logical inconsistency
Loops are not magic.
They are structured repetition systems.
12. A Critical Habit
Before writing any loop, ask:
“When does this loop stop?”
If you cannot answer that clearly,
Do not write the loop yet.
13. Practice Exercises
Implement the following using while:
- Print numbers from 1 to 100
- Increase by 2 each time
- Print only multiples of 5
- Count down from 100 to 0
Each one forces you to think about:
- Condition
- State
- State change
Final Summary
-
+=accumulates -
++increments by one - Loops depend on state change
- Infinite loops are state change failures
Top comments (0)