Do While Loop in C#

Do While Loop in C# with Examples

In this article, I am going to discuss the Do While loop in C# with Examples. Please read our previous articles, where we discussed While loop in C# Language with Examples. At the end of this article, you will understand what is the do-while loop and when and how to use a do-while loop in the C# language with examples.

Do while loop in C# Language:

The do-while loop is a post-tested loop. Using the do-while loop, we can repeat the execution of several parts of the statements. The do-while loop is mainly used in the case where we need to execute the loop at least once. The do-while loop is mostly used in menu-driven programs where the termination condition depends upon the end-user.

Syntax to use Do While Loop in C# Language:

Note that the do-while statement ends with a semicolon. The condition expression must be a boolean expression. The syntax to use the do-while loop is given below.

Syntax to use Do While Loop in C# Language

This is the syntax of the do-while loop. In the curly braces, we will write the statements which we want to process repeatedly and after closing the curly bracket, we will write while and give the condition of termination in the parenthesis. This condition must end with a semicolon. We don’t use the semicolon in the while loop but it is a must in do-while.

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.

Flow Chart of Do-While Loop:

In the do-while loop, first, it will perform processing without checking the condition. After processing, it will check for condition, if the condition is true then control goes to the process part as shown in the flowchart and if the condition is false then it will stop here, and control come out of the loop.

Flow Chart of Do-While Loop

Example to understand do while loop in C# Language:
using System;
namespace ControlFlowDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int j = 0;
            do
            {
                Console.WriteLine($"Value of variable j is: {j}");
                j++;
            } while (j <= 3);

            Console.ReadKey();
        }
    }
}
Output:

Example to understand do while loop in C# Language

Note: When you want to execute the loop body at least once irrespective of the condition, then you need to use the do-while loop.

Nested Do-While Loop in C# Language:

Using a do-while loop within do-while loops is said to be a nested do-while loop. The syntax to use the nested do-while loop in C# language is given below.

Nested Do-While Loop in C# Language

Example to Understand Nested do-while Loop in C# Language:
using System;
namespace ControlFlowDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            do
            {
                Console.WriteLine("I am from outer do-while loop");
                do
                {
                    Console.WriteLine("I am from inner do-while loop ");
                }
                while (1 > 10);
            }
            while (2 > 10);

            Console.ReadKey();
        }
    }
}
Output:

blank

While and do-while are almost the same. So, what is the difference? Which one do we use?

We can use any one of the loops. Only the difference is, in the while loop if the already condition will be false then it will not perform any processing, it will not execute the statement under curly braces. But in the do-while, if the already condition will be false then it will first perform processing and then check the condition. It means, at false condition do-while will be run for at least one time.

Use case scenario of the Do-While Loop in C#:

The do-while loop is used in menu-driven programs. To better explain this, consider the program below.

using System;
namespace ControlFlowDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            char choice;
            int menuoption;
            int a, b;
            do
            {
                Console.WriteLine("Press 1 for Addition, 2 for Subtraction, 3 for Mutiplication, 4 for Division" );
                menuoption = Convert.ToInt32(Console.ReadLine());
                switch (menuoption)
                {
                    case 1:
                        Console.WriteLine("Enter the value of two numbers");
                        a = Convert.ToInt32(Console.ReadLine());
                        b = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine($"Sum Is {a + b}");
                        break;
                    case 2:
                        Console.WriteLine("Enter the value of two numbers");
                        a = Convert.ToInt32(Console.ReadLine());
                        b = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine($"Difference Is {a - b}");
                        break;
                    case 3:
                        Console.WriteLine("Enter the value of two numbers");
                        a = Convert.ToInt32(Console.ReadLine());
                        b = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine($"Multiplication Is {a * b}");
                        break;
                    case 4:
                        Console.WriteLine("Enter the value of two numbers");
                        a = Convert.ToInt32(Console.ReadLine());
                        b = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine($"Division Is {a / b}");
                        break;
                    default:
                        Console.WriteLine("Invalid choice");
                        break;
                }
                Console.WriteLine("do you want to continue seleting the menu option please enter y for yes");
                
                choice = Convert.ToChar(Console.ReadKey());
            }
            while (choice == 'y');

            Console.ReadKey();
        }
    }
}
Output:

Do While Loop in C# Language with Examples

In the next article, I am going to discuss For Loop in C# with examples. Here, in this article, I try to explain Do While Loop in C# Language with Examples. I hope you enjoy this Do While Loop in C# Language with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.