This tutorial is about how to use loops in C#. Loop structures are used when there is a need to execute one or more statements again and again

Don't forget to check out our site http://howtech.tv/ for more free how-to videos!
http://youtube.com/ithowtovids - our feed
http://www.facebook.com/howtechtv - join us on facebook
https://plus.google.com/103440382717658277879 - our group in Google+

Introduction

Loop structures are used when there is a need to execute one or more statements again and again. In C#, the following loops can be used:
• The while loop
• The do---while loop
• The for loop


The while loop
When you need to execute a block statements for a definite number of times, depending on a condition, you use a while loop. The while loop in C# checks the condition before executing the statements in the loop. As the execution reaches the last statement, the control is passed back to the beginning of the loop and the condition is checked again. The loop continues to execute until the condition evaluates to false.

The following code snippet is an example of the while loop construct:
while (number less than 100)
{
Console.WriteLine("Value of Number = "+number);
number = number+10;
}
Note that the value of the number is incremented by 10 and the loop continues till the value of the number is less than 100. This prevent the loop from running infinitely.

The do---while loop
The do—while loop construct is similar to the while loop construct. Both continues looping until the while condition becomes false. The difference is the check of the conditional statement takes place at the end of the loop in a do---while loop construct. Hence, the statements within a do---while loop construct gets executed at least once.
Consider the following code snippet:
do
{
Console.WriteLine("Value of Number = "+number);
number = number+10;
} while (number less than 100);


The for loop

In C#, for loop is used to execute a block of statements for a specific number of times. The syntax of the C# for loop construct:
for(initialization;termination;increment/decrement)
{
Statements;
}
The initialization expression initializes the for loop construct. The termination expression is checked at the end of each loop execution and the increment/decrement is done after each loop execution. Note that all these components are optional.
The following code snippet is an example of the for loop in C#:
for(number=10; number less than 20; number++)
Console.WriteLine("Value of Number = "+number);