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();
    }
}

The Thinking Flow Behind This Code (Important)

1. The Essence of while

while (condition)
{
    // code to run repeatedly
}

Meaning:

Repeat this block as long as the condition stays true.

2. The Condition in This Example

while (word.Length < 15)

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';

Meaning:

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

This is equivalent to:

word = word + 'a';

This is the state change.

Without state change, the loop would never end.

char vs string (Important Here)

'a'   // char
"a"   // string
  • '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

Output:

cata
cataa
cataaa
...

It continues until the string reaches length 15.

One Critical Point

What if the input is already 15+ characters?

Example:

abcdefghijklmnop

Then:

while (word.Length < 15)

is false immediately.

So:

  • The loop never runs
  • The program jumps directly to:
Loop finished.

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");
}

Runs zero times.

do-while

do
{
    Console.WriteLine("Hello");
}
while (false);

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';

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)

But this condition is dangerous.

Think about why it is dangerous before using it.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

Is your startup’s check engine light on? Google Cloud’s VP explains what to do

Next Post

How to Select a Quality Management System: 7 Key Elements for Successful Implementation

Related Posts