DEV Community

Sabin Sim
Sabin Sim

Posted on

21. C# (while Loop 2)

Goal

  1. Ask the user to enter a word
  2. If the word length is less than 15
  3. Keep appending 'a'
  4. Stop once the length becomes 15 or more

Full Runnable Code (Copy & Paste)

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter a word:");
        string word = Console.ReadLine();

        while (word.Length < 15)
        {
            word += 'a';  // append one character
            Console.WriteLine(word);
        }

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

The Thinking Flow Behind This Code (Important)

1. The Essence of while

while (condition)
{
    // code to run repeatedly
}
Enter fullscreen mode Exit fullscreen mode

Meaning:

Repeat this block as long as the condition stays true.


2. The Condition in This Example

while (word.Length < 15)
Enter fullscreen mode Exit fullscreen mode

Interpretation:

Repeat while the length of word is less than 15.

This is a pre-check loop.

The condition is evaluated before entering the block.


3. What Happens Inside the Loop

word += 'a';
Enter fullscreen mode Exit fullscreen mode

Meaning:

  • Take the current word
  • Append the character 'a'
  • Store the result back into word

This is equivalent to:

word = word + 'a';
Enter fullscreen mode Exit fullscreen mode

This is the state change.

Without state change, the loop would never end.


char vs string (Important Here)

'a'   // char
"a"   // string
Enter fullscreen mode Exit fullscreen mode
  • 'a' is a single character
  • "a" is a string (a sequence of characters)

In this case, both work because C# can concatenate a char onto a string.

But in this lesson, 'a' is used intentionally to reinforce char.


Example Execution Flow

Input:

cat
Enter fullscreen mode Exit fullscreen mode

Output:

cata
cataa
cataaa
...
Enter fullscreen mode Exit fullscreen mode

It continues until the string reaches length 15.


One Critical Point

What if the input is already 15+ characters?

Example:

abcdefghijklmnop
Enter fullscreen mode Exit fullscreen mode

Then:

while (word.Length < 15)
Enter fullscreen mode Exit fullscreen mode

is false immediately.

So:

  • The loop never runs
  • The program jumps directly to:
Loop finished.
Enter fullscreen mode Exit fullscreen mode

This is the defining behavior of while.


Why do-while Exists

while means:

Check first → then execute.

do-while means:

Execute once → then check.


Comparison

while

while (false)
{
    Console.WriteLine("Hello");
}
Enter fullscreen mode Exit fullscreen mode

Runs zero times.


do-while

do
{
    Console.WriteLine("Hello");
}
while (false);
Enter fullscreen mode Exit fullscreen mode

Runs at least once.


The Question You Must Always Ask in Loops

“Will this loop eventually end?”

If the answer is unclear, you are risking an infinite loop.


What You Must Understand Right Now

A while loop is:

  • Condition-based repetition
  • Terminates the moment the condition becomes false
  • Requires state change inside the loop

In this example, the termination mechanism is:

word += 'a';
Enter fullscreen mode Exit fullscreen mode

It increases word.Length until it reaches 15.


Practice Task

Modify the program so it stops not at 15, but when the string becomes exactly 20 characters.

Hint:

while (word.Length != 20)
Enter fullscreen mode Exit fullscreen mode

But this condition is dangerous.

Think about why it is dangerous before using it.

Top comments (0)