In this article I am going to discuss about C# Do While Loop in detail bet before that if you had not read my previous article of C# While loop then must read it. Do while loop is also known as exit controlled or post tested loop. We will also see why do while loop is called exit controlled or post tested loop so if you don’t know then don’t worry and read this full article for clearity of each and every thing about do while loop.

Introduction to C# Do While Loop

In C# do while loop is the variant of while loop in csharp but the only difference is that while loop will first check the condition and if condition is true then statement is executed but in do while loop first it will execute the code and then checks the condition. Therefore it is called post-tested loop because it will test the code after the execution of the statement. In simple words the do while loop is the same as while loop except that it executes the code block at least once. The do-while loop starts with the do keyword followed by a code block and a boolean expression with the while keyword. The do while loop stops execution exits when a boolean condition evaluates to false. The syntax for do while loop is given :

do
{
    //code block

} while(condition);

C# Program to Explain Do While Loop

using System;
namespace DoWhileLoop
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 1;
            do
            {
                Console.WriteLine("The Number is : {0} ",number);
                number++;
            } while (number <= 5);

            Console.ReadKey();
        }
    }
}
Output:
The Number is : 1 
The Number is : 2 
The Number is : 3 
The Number is : 4 
The Number is : 5 

In above program we have initialized one variable and started a do while loop without checking the condition so it will print first time without checking any condition and at the last of statement we are checking the condition using while keyword.

C# Nested Do While Loop

As you already know nesting means using one loop inside another loop. We had already covered the nested loop programs in our previous article of for loop and while loop. Here I am going to make one program to demonstrate the use of nested do while loop.

using System;
namespace NestedDoWhileLoop
{
    class Program
    {
        static void Main(string[] args)
        {
            do
            {
                Console.WriteLine("Outer do while loop");
                do
                {
                    Console.WriteLine("Inner do while loop ");
                }
                while (1>10);
            }
            while (1 > 10);

            Console.ReadKey();
        }
    }
}
Output:
Outer do while loop
Inner do while loop 

In above program we have using one outer do while loop and inside the do while loop we again using another do while loop. One thing you have noticed in above program that in while conditions I have written the false conditions. But inspite of false conditions our program has executed once and printed the outer and inner statement one times. That proves do while loop will first do means first execute the statement then check the condition. First time it has printed the statements and for second time it has checked for the condition but this time conditions failed and loop has been terminated.

Difference between While Loop and Do While Loop

As we have already discussed the do while loop is one of the variant of while loop which means the syntax of do while loop and while loop is almost similar because in both of the loop we use while keyword for checking the condition. But while loop is entry controlled loop which means first check the condition and if the condition is true then execute the statement but do while loop is exit controlled loop which means it will execute the statement first and then check for the condition. Therefore when we are using do while loop it will guaranteed that the statement will be definitely execute at least one time.

When to Use C# Do While Loop?

After understanding the concept of do while loop another question comes into our mind is when to use csharp do while loop in our program. So here answer is different but the use case is similar because most of the times we will be using do while loop when we have to execute our program at least one time and then want to check the condition. If the condition is true then statement is executed else not. You can understand my wordings after this program that is given below:

using System;
namespace DoWhileLoopUseCase
{
    class Program
    {
        static void Main(string[] args)
        {
            char Choice;
            int MenuOption;
            int Number1, Number2;
            do
            {
                Console.WriteLine("Enter the value of two numbers");
                Number1 = Convert.ToInt32(Console.ReadLine());
                Number2 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Now Press 1 for Addition");
                Console.WriteLine("Now Press 2 for Subtraction");
                Console.WriteLine("Now Press 3 for Multiplication");
                Console.WriteLine("Now Press 4 for Division");
               
                MenuOption = Convert.ToInt32(Console.ReadLine());
                switch (MenuOption)
                {
                    case 1:
                        Console.WriteLine($"Sum Is {Number1 + Number2}");
                        break;
                    case 2:
                        Console.WriteLine($"Difference Is {Number1 - Number2}");
                        break;
                    case 3:
                        Console.WriteLine($"Multiplication Is {Number1 * Number2}");
                        break;
                    case 4:
                        
                        Console.WriteLine($"Division Is {Number1 / Number2}");
                        break;
                    default:
                        Console.WriteLine("Invalid choice");
                        break;
                }
                Console.WriteLine("Please Enter Y to continue and any keys to terminate");

                Choice = Convert.ToChar(Console.ReadLine());
            }
            while (Char.ToUpper(Choice) == 'Y');
        }
    }
}
Output:
Enter the value of two numbers
5
7
Now Press 1 for Addition
Now Press 2 for Subtraction
Now Press 3 for Multiplication
Now Press 4 for Division
3
Multiplication Is 35
Please Enter Y to continue and any keys to terminate
n

In above program I have made one small calculator like application which takes two number as input from user and then asking for choice of operation and as per the choice doing the calculation. At last you see it will asking if we want to continue then press y else enter any keys. If we press y then this program again ask for two numbers then choice of operation and only stops if we enter anything except y. This thing is done with the do while loop which will check the condition at last after executing the statement first time without checking the condition.

Conclusion

In this C# tutorials article we have discussed the C# do while loop in detail and also discussed the difference between while loop and do while loop, when to use do while loop. Hope you understood the working of do while loop in csharp. Again I’m say if you want to be a good developer then always first try to solve the problem in your notebook and after making the approach then go to compiler. With this your problem solving skills will be enhanced and helps in brainstorming. Hope you like this article. In my next article I’m discussing the foreach loop in detail.