×

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# 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# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as...

3 minutes read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

C# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

3 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# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 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# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 minutes read.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 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.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

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.

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 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# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute 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.

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.