C# While Loop

C# While Loop

1 - C# While Loop

The C# while loop continually executes a block of statements until a specified expression evaluates to false . The expression is evaluated each time the loop is encountered and the evaluation result is true, the loop body statements are executed.

Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code more readable.

while loops repeatedly execute a body of code while a bool expression is true.

The expression is tested before the body of the loop is executed.

For example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            while (i < 3)
            {
                Console.WriteLine(i);
                i++;
            }
            Console.ReadLine();
        }
    }
}

Output:

C# While Loop

*** In C#, while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop than for loop.

 

2 - Syntax of C# While Loop

The syntax of a while loop in C# is:

while(condition)
{
   statement(s); //code to be executed  
} 

Like if statement the while statement evaluates the expression, which must return a boolean value . If the expression evaluates to true, the while statement executes the statement(s) in the while block . The while statement continues testing the expression and executing its block until the expression evaluates to false.

 

3 - C# While Loop Flow Chart Diagram

C# While Loop Flow Chart Diagram

Here, key point of the while loop is that the loop might not ever run. When the condition is tested and the result is false, the loop body is skipped and the first statement after the while loop is executed.

 

4 - C# While Loop Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = 1;
            while (count <= 4)
            {
                Console.WriteLine("The value of i is : " + count);
                count = count + 1;
            }
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

output:

When the above code is compiled and executed, it produces the following result:

C# While Loop Example

The while loop executes a statement or a block of statements until a specified expression evaluates to false . The above C# while loop example shows the loop will execute the code block 4 times.

 

5 - C# Nested While Loop

C# allows while loops inside another while loop, as shown below. However, it is not recommended to use nested while loop because it makes it hard to debug and maintain.

int i = 0, j = 1;

while (i < 2)
{
    Console.WriteLine("i = {0}", i);
    i++;

    while (j < 2)
    {
        Console.WriteLine("j = {0}", j);
        j++;
    }
}

Output:

i = 0
j = 1
i = 1

 

6 - C# Nested While Loop Example

In c#, we can use one while loop within another while loop to implement applications based on our requirements.

Following is the example of implementing nested while loop in the c# programming language:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;
            while (i < 4)
            {
                Console.WriteLine("i value: {0}", i);
                i++;
                int j = 1;
                while (j < 2)
                {
                    Console.WriteLine("j value: {0}", j);
                    j++;
                }
            }
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

If you observe the above example, we used one while loop within another while loop to achieve nested while loop functionality in our application based on our requirements.

When we execute the above c# program, we will get the result below:

C# Nested While Loop Example

7 - C# While Loop with Break Statement

In c#, we can exit or terminate the execution of a while loop immediately by using a break keyword.

Following is the example of using the break keyword in a while loop to terminate the loop's execution in the c# programming language.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;
            while (i < 4)
            {
                Console.WriteLine("i value: {0}", i);
                i++;
                if (i == 2)
                    break;
            }
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

If you observe the above example, whenever the variable (i) value becomes 2, we terminate the loop using the break statement.

When we execute the above c# program, we will get the result below:

C# While Loop with Break Statement

This is how we can use break statements with a while loop to terminate loops' execution based on our requirements.

 

8 - C# Infinitive While Loop Example

We can also create infinite while loop by passing true as the test condition.

*** Press ctrl+c to Exit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("Infinitive While Loop");
            }
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# Infinitive While Loop Example
Infinitive While Loop 
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
ctrl+c