Do While in C#


The do - while loop is similar to the while loop in that the loop continues as long as the specified loop condition remains true .

 The main difference is that the condition is checked at the end of the loop — which contrasts with the while loop and the for loop, where the condition is checked at the beginning of the loop.

The do - while loop statement is always executed at least once.

C# Do While Example

using System;

namespace ProgramCall
{

    class Sample
    {
        static void Main()
        {

            int A, B, Ch;
            string Continue;

            do
            {

                Console.WriteLine("Enter Two Integers");

                A = int.Parse(Console.ReadLine());

                B = int.Parse(Console.ReadLine());

                Console.Write("Enter Your Choice (1 - Add/2 - Sub/3 - Mul/ 4 - Div) : ");

                Ch = int.Parse(Console.ReadLine());

                switch (Ch)
                {
                    case 1:

                        Console.WriteLine("Sum Is {0}", A + B);
                        break;
                    case 2:

                        Console.WriteLine("Difference Is {0}", A - B);
                        break;
                    case 3:

                        Console.WriteLine("Product Is {0}", A * B);
                        break;

                    case 4:

                        Console.WriteLine("Ratio Is {0}", A / B);
                        break;

                    default:

                        Console.WriteLine("Wrong Choice");
                        break;
                }

                Console.Write("Do You Want To Continue? (Y/N) : ");
                Continue = Console.ReadLine();
            } while (Continue != "N" && Continue != "n");
        }
    }
}

OUTPUT
----------
Enter Two Integers
65
86
Enter Your Choice (1 - Add/2 - Sub/3 - Mul/ 4 - Div) : 1
Sum Is 151
Do You Want To Continue? (Y/N) : y
Enter Two Integers
45
21
Enter Your Choice (1 - Add/2 - Sub/3 - Mul/ 4 - Div) : 4
Ratio Is 2
Do You Want To Continue? (Y/N) :



do while in c#.NET c# do while example do while c# loops c# while do