Loop In C#
Loops execute one or more lines of code repetitively. The following loop constructs are supported by C#:
- While Loop
- Do-While Loop
- For Loop
- Interrupting Loops
The While Loop
The while loop construct executes a statement or block of statements for a definite number of times, depending on the condition. The while
statement always checks the condition before executing the statements in the loop.
When the execution reaches the last statement in the loop, control returns to the beginning of the loop. If the condition remains true then the
statement within the loop is executed again. Execution of the statement continues until the condition in the loop evaluates to false.
As a simple example, consider the following code for calculating the amount of money in a bank account after 10 years, assuming that interest
is paid each year and no other money flows into or out of the account.
Code
double balance=1000;
double interestRate=1.05;// 5% interest /Year
balance*= interestRate;
balance*= interestRate;
balance*= interestRate;
balance*= interestRate;
balance*= interestRate;
balance*= interestRate;
balance*= interestRate;
balance*= interestRate;
balance*= interestRate;
balance*= interestRate;
Writing same code 10 times seems a bit wasteful, and what if you change the duration from 10 to 40 years?
You need to manually copy that code 40 times. So the same code can be replaced with the following code:
class Program
{
static void Main(string[] args)
{
double balance, interestRate, targetBalance;
Console.WriteLine("What is your current balance?");
balance = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("What is your current annual interest rate (in %)?");
interestRate = 1 + Convert.ToDouble(Console.ReadLine()) / 100.0;
Console.WriteLine("What balance would you like to have?");
targetBalance = Convert.ToDouble(Console.ReadLine());
int totalYears = 0;
while (balance < targetBalance)
{
balance *= interestRate;
++totalYears;
}
Console.WriteLine("In {0} year{1} you'll have a balance of {2}.",
totalYears, totalYears == 1 ? "" : "s", balance);
Console.ReadKey();
}
}
Execute the code and enter some values

In the preceding code we have created a double variable balance, interestRate, and targetBalance.
double balance, interestRate, targetBalance;
Console.WriteLine("What is your current balance?");
balance = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("What is your current annual interest rate (in %)?");
interestRate = 1 + Convert.ToDouble(Console.ReadLine()) / 100.0;
Console.WriteLine("What balance would you like to have?");
targetBalance = Convert.ToDouble(Console.ReadLine());
Using the code above we are just accepting a value for the variable balance, interestRate and targetBalance.
int totalYears = 0;
while (balance < targetBalance)
{
balance *= interestRate;
++totalYears;
}
This code simply repeats the simple annual calculation of the balance with a fixed interest rate as many time as necessary for the balance to
satisfy the terminating condition. We are maintaining a count of how many years has been accounted for by incrementing the totalyears as a
counter variable with each loop of the cycle.
Do-While Loop
The Do-While loop is similar to the While Loop. Both iterate until the specified condition becomes false.
However, in the Do-While loop, the body of the loop is executed at least once and the condition is evaluated for subsequent iteration. And the
reason behind that is, the Boolean test in the While loop is done at the start of the loop, not at the end. But in the While-Loop the Boolean test
in the while loop is done at the end of the loop.
The difference between the While and Do-While loop is shown in the following figure.

Code
class Program
{
static void Main(string[] args)




Aditya KumarPosted Nov 26, 2016, 5:24 AM
A very good airticle. Thanks for sharing.
Aditya KumarPosted Nov 26, 2016, 5:24 AM
Nice airticle sir. Thanks for sharing.