Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - DO - WHILE LOOP


The do - while loop is just like the while loop with a little difference.


This loop will execute the code block at least once, because it gets into the loop first, executes the block inside the loop. Then it checks for the conditional part i.e. while(num <= 5 ).


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        int x = 1;
        do {
            System.Console.WriteLine("Hello World");
            x = x + 1;
        } while (x <= 5); 
    }
}


Output :



  Hello World
  Hello World
  Hello World
  Hello World
  Hello World

Now if you see the output, Hello World is printed 5 times. So, how is it different from while loop.


Let us understand with the below example. Where we will provide a while loop and a do - while loop, with a false condition.


Sounds tough?


Let us simplify it.


Comparison of 'while' loop and 'do - while' loop


At first, let us write the code with while loop.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        int x = 1;
        while (x >= 5) 
        {
            System.Console.WriteLine("Hello World");
            x = x + 1;
        }
    }
}


Output :




Now, if you look at the output, Hello World is NOT printed on the screen.


Because, the initial value of x is 1.

C_Sharp

And the condition in the while loop is,


while (x >= 5)

Which says enter the loop if the value of x is greater than or equal to 5. And in this case the value of x is 1. So, it won't enter the loop.


You can consider the conditional part of the while loop as a security guard. Who will only let you enter, if the condition is satisfied.

C_Sharp

And in this case the security guard will not let you enter, because the value of x is 1. And the condition says, only let them for whom the value of x is greater than(or equal to) 5.


Fair enough.


Now, let us write the same example using do - while loop.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        int x = 1;
        do {
            System.Console.WriteLine("Hello World");
            x = x + 1;
        } while (x >= 5);
    }
}


Output :



  Hello World

Now, if you look at the output, Hello World is printed one time on the screen.


This is because of the do - while loop.


do {
	System.Console.WriteLine("Hello World");
	x = x + 1;
} while (x >= 5);

In do - while loop, the conditional part is at the end of the loop.


Once again, let us consider the conditional part of the do - while loop as a security guard. Who will only let you enter, if the condition is satisfied.


But the security guard is not at the entry point. He is at the exit point of the loop.


Now, since there is no restriction at the entry point, you enter the do - while loop.

C_Sharp

And Hello World is printed.

C_Sharp

Then the value of x is incremented. And you come to the end of the loop.

C_Sharp

And this is where the security catches you. He finds out the value of x is 2, which is less than 2.


So the condition is not satisfied. And he puts you out of the loop.


So, in simple words, since there is no restriction at the beginning of the do - while loop, no matter what the condition is, the control gets into the do - while loop at least once.