C# Do-While Loop

C# Do-While Loop

The while statement in C# allows for the execution of a single statement or a block of statements as long as a specified expression evaluates to true. It serves as a loop construct, repeatedly executing the associated code until the condition becomes false. This provides flexibility in controlling program flow based on dynamic conditions.

 

1 - C# - Do...While Loop

However, there are scenarios where you may want to ensure that the loop is executed at least once, even if the condition initially evaluates to false. This is where the do..while loop comes into play. 

This construct first executes the statements within the loop and then evaluates the specified condition. If the condition is true, the loop continues to execute, and if it becomes false, the loop terminates.

The do..while loop guarantees that the associated code block is executed at least once before checking the condition. This can be useful when you need to perform an initial action or validation before entering the loop.

do-while loops test the expression after the statement block has executed.

do-while loops ensure that the block is always executed at least once.

Here's the preceding example rewritten with a do-while loop:

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;
            do
            {
                Console.WriteLine(i);
                i++;
            } while (i < 3);

            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

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

C# - Do...While Loop

2 - Syntax of C# Do…While Loop

The syntax of a do...while loop in C# is:

do
{
   statement(s);

}while( condition );

 

3 - C# Do…While Loop Flow Chart Diagram

Following is the pictorial representation of the do-while loop process flow in the c# programming language.

C# Do…While Loop Flow Chart Diagram

Now we will see how to use the do-while loop in the c# programming language with examples.

 

4 - C# Do…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)
        {
            /* local variable definition */
            int a = 10;

            /* do loop execution */
            do
            {
                Console.WriteLine("value of a: {0}", a);
                a = a + 1;
            }
            while (a < 20);
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

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

C# Do…While Loop Example

 

5 - C# Nested Do…While

The do-while loop can be used inside another do-while loop.

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;

            do
            {
                Console.WriteLine("Value of i: {0}", i);
                int j = i;

                i++;

                do
                {
                    Console.WriteLine("Value of j: {0}", j);
                    j++;
                } while (j < 2);

            } while (i < 2);
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# Nested Do…While

 

6 - C# Do-While Loop with Break Statement

Use break or return to exit from the do while loop.

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;

            do
            {
                Console.WriteLine("i = {0}", i);
                i++;

                if (i > 5)
                    break;

            } while (i < 10);
            Console.WriteLine("Press Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

Output:

C# Do-While Loop with Break Statement

 

7 - C# Infinitive Do…While Loop

In C#, if you pass true in the do-while loop, it will be infinitive do-while loop.

Syntax:

do{  
//code to be executed  
}while(true); 

 

8 - C# Infinitive Do…While Loop Example

*** 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)
        {
            do
            {
                Console.WriteLine("Infinitive do-while Loop");
            } while (true);
           
        }
    }
}

Output:

C# Infinitive do-while Loop Example
Infinitive do-while Loop 
Infinitive do-while Loop
Infinitive do-while Loop
Infinitive do-while Loop
Infinitive do-while Loop
ctrl+c