C# While Loop

In C#, the while loop is a control flow statement that allows you to repeatedly execute a block of code as long as a specified condition is true. The general syntax of a while loop in C# is as follows:

while (condition)
{
    // Code to be executed while the condition is true
    // This code block will repeat until the condition becomes false
}

Here’s a breakdown of how the while loop works:

  1. The condition is a Boolean expression that determines whether the loop should continue or terminate. It is evaluated before each iteration of the loop.
  2. If the condition is initially false, the code block inside the while loop will not be executed at all.
  3. If the condition is true, the code block inside the while loop will be executed. After completing the code block, the program will return to the beginning of the loop and evaluate the condition again.
  4. If the condition is still true, the code block will be executed again. This process repeats until the condition becomes false.
  5. Once the condition becomes false, the program exits the while loop and continues with the execution of the subsequent code.

Here’s an example that demonstrates a simple while loop in C#:

int count = 0;

while (count < 5)
{
    Console.WriteLine("Count: " + count);
    count++;
}

Console.WriteLine("Loop finished.");

In this example, the while loop will iterate as long as the value of count is less than 5. Inside the loop, it will print the current value of count, increment count by 1, and repeat the process. Once count becomes 5, the condition becomes false, and the loop terminates. Finally, the program will print “Loop finished.”

C# Nested While Loop Example:

Certainly! Here’s an example of a nested while loop in C#:

int outerCount = 1;

while (outerCount <= 3)
{
    int innerCount = 1;

    while (innerCount <= 5)
    {
        Console.WriteLine("Outer Count: " + outerCount + ", Inner Count: " + innerCount);
        innerCount++;
    }

    outerCount++;
}

Console.WriteLine("Nested loop finished.");

In this example, we have a nested while loop. The outer loop iterates as long as the outerCount is less than or equal to 3. Inside the outer loop, we have an inner loop that iterates as long as the innerCount is less than or equal to 5.

During each iteration of the inner loop, it will print the current values of both outerCount and innerCount. After printing, the innerCount is incremented by 1. Once the innerCount reaches 6, the inner loop exits, and the control goes back to the outer loop. The outer loop continues until the outerCount becomes 4, and then it terminates.

Finally, after both loops have finished, the program will print “Nested loop finished.”

The output of this example will be:

Outer Count: 1, Inner Count: 1
Outer Count: 1, Inner Count: 2
Outer Count: 1, Inner Count: 3
Outer Count: 1, Inner Count: 4
Outer Count: 1, Inner Count: 5
Outer Count: 2, Inner Count: 1
Outer Count: 2, Inner Count: 2
Outer Count: 2, Inner Count: 3
Outer Count: 2, Inner Count: 4
Outer Count: 2, Inner Count: 5
Outer Count: 3, Inner Count: 1
Outer Count: 3, Inner Count: 2
Outer Count: 3, Inner Count: 3
Outer Count: 3, Inner Count: 4
Outer Count: 3, Inner Count: 5
Nested loop finished.

As you can see, the inner loop is executed 5 times for each iteration of the outer loop.

C# Infinitive While Loop Example:

An infinite while loop in C# is a loop that continues indefinitely, without an explicit exit condition. It is often used when you want a loop to run continuously until an external event or condition occurs that breaks the loop. Here’s an example of an infinite while loop in C#:

while (true)
{
    // Code to be executed indefinitely
}

In this example, the while loop has a condition of true, which means the loop will continue executing indefinitely since the condition always evaluates to true. To exit the loop, you would need to use a break statement or some other control flow mechanism within the loop.

However, it’s important to note that an infinite loop should be used with caution, as it can lead to programs getting stuck in an infinite loop and becoming unresponsive. It’s typically recommended to have a way to exit the loop based on specific conditions or events.

Here’s an example that demonstrates an infinite while loop with a condition for termination:

while (true)
{
    Console.WriteLine("Enter a number (or type 'exit' to quit):");
    string input = Console.ReadLine();

    if (input.ToLower() == "exit")
    {
        break; // Exit the loop when the user enters 'exit'
    }

    // Process the input or perform other operations
    // ...

    Console.WriteLine("Input processed.");
}

Console.WriteLine("Loop finished.");

In this example, the loop will run indefinitely until the user enters “exit” as input. When the user enters “exit”, the break statement is executed, which causes the loop to terminate, and the program continues with the execution of the subsequent code.