20. C# (Infinite Loops)

# csharp# programming# learning# career
20. C# (Infinite Loops)Sabin Sim

0. The Real Goal of This Lesson A while loop repeats based on a condition, but what...

0. The Real Goal of This Lesson

A while loop 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);
}
Enter fullscreen mode Exit fullscreen mode

This prints:

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

Why?

Because the state (number) changes every iteration.


2. The += Operator (Compound Assignment)

This line:

number = number + 1;
Enter fullscreen mode Exit fullscreen mode

Can be simplified to:

number += 1;
Enter fullscreen mode Exit fullscreen mode

It means:

Take the current value of number,
add 1,
and store the result back into number.

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

2
4
6
8
10
Enter fullscreen mode Exit fullscreen mode

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

What happens?

The console keeps printing:

0
0
0
0
...
Enter fullscreen mode Exit fullscreen mode

The program never stops.


6. Why Did This Happen?

Initial state:

number = 0
Enter fullscreen mode Exit fullscreen mode

Inside the loop:

number *= 2;
Enter fullscreen mode Exit fullscreen mode

0 × 2 = 0

The state does not change.

Condition:

0 < 10 → always true
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Now the flow becomes:

1 → 2 → 4 → 8 → 16
Enter fullscreen mode Exit fullscreen mode

When number becomes 16:

16 < 10 → false
Enter fullscreen mode Exit fullscreen mode

The loop exits correctly.

Termination restored.


9. The ++ Operator (Increment Operator)

This:

number += 1;
Enter fullscreen mode Exit fullscreen mode

Can be shortened to:

number++;
Enter fullscreen mode Exit fullscreen mode

It means:

Increase number by 1.

Same state change.

Cleaner syntax.


10. Pre-Increment vs Post-Increment (Simplified for Now)

number++;
++number;
Enter fullscreen mode Exit fullscreen mode

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:

  1. Print numbers from 1 to 100
  2. Increase by 2 each time
  3. Print only multiples of 5
  4. 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