×

C# 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);   
}  
}

Related Topics

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes read.

C# Keywords

Keywords are the reserved words with special meaning. They can’t be used as variables and Identifiers. If you want to use them as identifiers, you must use ‘@’ character prior...

1 minute read.

C# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 minutes read.

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 minutes read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# Namespace

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace...

1 minute read.

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes read.

C# Call by reference

In C#, we can use ref keyword to pass the reference to the original value instead of the copy of the original value. the changes that are made in the...

2 minutes read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.

C# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 minutes read.

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.