C# |C Sharp do While loop

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the number of iteration is not known in advance and you want to run the loop at least once.

Syntax:
do
{
//statements
}while(<Condition>);
Let's see a simple example of do while loop.
using System;
public class DoWhileLoopExample1{
            public static void Main (string[] args)
            {
            int i=1;
            do
            {
            Console.WriteLine(i);
            i++;
            }while(i<=10);
}
}
C# Do while loop example: A user responsive program
using System;
public class DoWhileLoopExample2
{
            public static void Main (string[] args)
            {
            int n,choice=1;
            do
            {
            n=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(n);
            Console.WriteLine("enter 1 to input more, 0 to exit");
            choice=Convert.ToInt32(Console.ReadLine());
            }while(choice!=0);
            }
}
Output:
45
45
enter 1 to input more, 0 to exit
1
70
70
enter 1 to input more, 0 to exit
0
Nested do while loop: Like for and while loop, we can nest do while loop also. The outer loop will run n number of times for each iteration of the outer loop. Let's see a simple example of nested while loop:
using System;  
public class DoWhileExample  
{  
public static void Main(string[] args)  
{  
int i=1;    
do{  
int j = 1;  
do{  
Console.WriteLine(i+""+j);  
j++;  
} while (j <= 3) ;  
i++;  
} while (i <= 3) ;    
}  
}
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Infinite do while loop: An infinite do while loop is the one which iterate infinite times means which is never going to stop by itself. We can make do while loop an infinite loop by providing a condition in the while statement which is never going to fail. Let's see a simple example of infinite do while loop
using System;  
public class WhileExample  
{  
public static void Main(string[] args)  
{  
do{  
Console.WriteLine("Infinitive do-while Loop");  
} while(true);   
}  
}