×

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# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

1 minute 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# 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# 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# 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# 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# 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# 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# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 minutes read.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

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

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using...

3 minutes read.

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 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# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes read.

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.