19. C# (while Loop)

# csharp# programming# learning# career
19. C# (while Loop)Sabin Sim

0. The Real Goal of This Lesson A while loop is a mechanism that sends execution flow...

0. The Real Goal of This Lesson

A while loop is
a mechanism that sends execution flow back upward as long as a condition remains true.

If you do not understand this:

  • Infinite loops happen
  • Condition logic breaks
  • CPU usage spikes to 100%

And your program loses control.

This is not about syntax.

It is about controlling repetition safely.


1. Start with the Structure

while (condition)
{
    // repeated code
}
Enter fullscreen mode Exit fullscreen mode

In plain language:

“As long as the condition is true,
execute this block repeatedly.”

The condition is the gatekeeper.


2. A Simple Example (You Must Run This Yourself)

using System;

class Program
{
    static void Main()
    {
        int number = 0;

        while (number < 10)
        {
            number++;
            Console.WriteLine($"Number is {number}");
        }

        Console.WriteLine("Loop finished.");
        Console.ReadKey();
    }
}
Enter fullscreen mode Exit fullscreen mode

Run it.

Do not just read it.


3. Track the Execution Flow Precisely

Initial state:

number = 0
Enter fullscreen mode Exit fullscreen mode

First Condition Check

while (number < 10)
Enter fullscreen mode Exit fullscreen mode

0 < 10 → true

→ Enter loop.


Inside the Loop

number++;   // number becomes 1
Console.WriteLine("Number is 1");
Enter fullscreen mode Exit fullscreen mode

Flow Returns Upward

Condition is checked again:

1 < 10 → true

→ Execute again.

This continues.

Execution is not moving forward only.

It is cycling.


4. Understand the Exact Termination Moment

When number becomes 9:

9 < 10 → true

Enter loop
number++ → 10 is printed.

Now check again:

10 < 10 → false

The loop stops.

Execution continues below the loop.

Loop finished.
Enter fullscreen mode Exit fullscreen mode

That exact moment defines control.


5. The Core Structure of while

A while loop always behaves like this:

[Check condition]
      ↓ true
[Execute block]
      ↓
[Check condition]
      ↓ false
[Exit loop]
Enter fullscreen mode Exit fullscreen mode

Important:

The condition is checked before entering the block.

This is called a pre-check loop.

It may execute zero times.


6. The Critical Danger — Infinite Loops

Consider this code:

int number = 0;

while (number < 10)
{
    Console.WriteLine(number);
}
Enter fullscreen mode Exit fullscreen mode

What is missing?

There is no state change.

number never increases.

Result:

number = 0
0 < 10 → true
Print 0
Check again → still 0 < 10
Print 0
Repeat forever
Enter fullscreen mode Exit fullscreen mode

This creates an infinite loop.

The CPU keeps executing.

The program never exits.


7. The Structural Model (Think in Components)

You prefer structured thinking.

So break a while loop into three required components:

Component Purpose
Condition Defines when to continue
State variable Value used inside condition
State change Modifies the variable so condition eventually becomes false

All three must exist.

Condition
State variable
State change
Enter fullscreen mode Exit fullscreen mode

If one is missing:

  • Infinite loop
  • Immediate termination
  • Logical inconsistency

This is not optional.

It is structural.


8. Understanding Through Debugging (Rider)

To truly understand:

  1. Place a breakpoint on the while line
  2. Use Step Into (F11)
  3. Observe the value of the state variable

Watch:

  • When the condition is evaluated
  • When the state changes
  • When the loop exits

Flow becomes visible.

And once visible, it becomes predictable.


9. Why while Matters in Real Applications

In a TODO or Calculator app:

Requirement:

If input is invalid, ask again.

Structurally:

Input
Validate
If invalid → ask again
Enter fullscreen mode Exit fullscreen mode

Which becomes:

while (inputIsInvalid)
{
    AskAgain();
}
Enter fullscreen mode Exit fullscreen mode

This is not just repetition.

It is controlled validation flow.


10. One-Line Summary

A while loop sends execution flow back upward
as long as its condition remains true,
and it must contain logic that eventually makes that condition false.