C# Do while loop

Tutorials PanelPosted by

In this lesson, we will learn about the do while loop and its importance in C#. Examples with explanation will be provided to better understand this topic.

‘do while’ loop in C#

The do-while loop is another type of loop in C#. It’s similar to the while loop, however the block of code will be executed before checking the condition of the loop.

Do while loop syntax

            do
            {
                //execute code block
            }
            while (boolean expression);

Looking at the syntax above, the code block is executed at least one time before the expression is checked.

Do while loop flowchart

c# do while loop flowchart

Example of do while loop

        static void Main(string[] args)
        {
            int i = 0; do
            {
                Console.WriteLine("Value of i: {0}", i); i++;
            }
            while (i < 5);

            Console.ReadKey();
        }

Output

c# do while loop

break inside ‘do while’ loop

As we learned in previous lesson, you can break out a loop if a specific condition met. For example, you want to exit a loop when you find the Customer you are looking for; there is no reason to keep the loop going if a condition is met.

Example of break in ‘do while’ loop

   static void Main(string[] args)
        {
            int i = 0;

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

                if (i == 5)
                {
                    break;
                }
                i++;

            }
            while (i < 10);

            Console.ReadKey();
        }

c# do while loop

continue inside ‘do while’ loop

The ‘continue’ work similar to ‘break’ in a ‘do while’ loop. However the continue will not exet the loop but it will skip the loop body and move to the next iteration.

Example of continue in ‘do while’ loop

   static void Main(string[] args)
        {
            int i = 0;

            do
            {
                if (i == 5)
                {
                    i++;
                    continue;// move to next iteration

                }
                Console.WriteLine("Value of i: {0}", i);
                i++;
            }
            while (i < 10);

            Console.ReadKey();
        }

Output

c# do while loop

Nested ‘do while’ loop in C#

Nested do while loops do exists in C#. A loop can be inside another loop in case you are looping thought more than one element. For example, you can loop through a table rows, then have another loop to go through the columns of that table.

Example of Nested ‘do while’ loop

    static void Main(string[] args)
        {
            int i = 0;
            do
            {
                int j = 0;
                do
                {
                    Console.WriteLine("i=" + i + ", j=" + j);
                    j++;
                } while (j < 2);
                i++;

            } while (i < 2);

            Console.ReadKey();
        }

c# nested do while loop

More Examples of ‘do while’ loop in C#

Example I: Do while loop with array

static void Main(string[] args)
{
string[] name = new string[] { "'ASP.NET'", "'CSharp'", "'Java'", "'Pascal'", "'R'","'Visual Basic'" };
int i = 0;
do
{
Console.WriteLine(name[i].ToString() + " is a programming language.");
Console.WriteLine("");
i++;
}
while (i<= name.Length-1);
Console.ReadKey();
}

Output

c# do while loop

In the example above, the loop will go through the array ‘name’ and print the value of the current index as long as ‘i’ is less than or greater the length of the array – 1 (array index starts at 0, so the –1 is important to avoid ‘System.IndexOutOfRangeException’).

Example II: Find factorial using do while loop

 static void Main(string[] args)
        {
            {
                Console.WriteLine("Please enter your number: ");
                int n = int.Parse(Console.ReadLine());
                int i = 1;
                int factorial = 1;
                do
                {
                    factorial *= i;
                    i++;
                }
                while (i <= n);
                Console.WriteLine(factorial);
            }

            Console.ReadKey();
        }

Output

c# do while loop

Factorial of a number is the product of all positive integers less than or equal that number. Example: factorial of 5 is 1 x 2 x 3 x 4 x 5 = 120

Leave a Reply