Introduction
In C#, iteration statements are used to repeat a block of code several times or until a specific condition is met. The process of repeating a task repeatedly is called looping or iteration. There are four types of iteration statements in C#: For, Foreach, Do, and While. This article teaches how to use these iteration statements in C#.
C# for statement
The for statement in C# is used to execute a block of code a specific number of times. It consists of three parts: the initialization statement, the condition expression, and the iterator statement.
The general syntax of a for the statement in C# is.
for (initialization statement; condition expression; iterator statement)
{
// code to be executed
}
The initialization statement is executed once before the loop starts and is used to declare and initialize any variables used in the loop.
The condition expression is evaluated before each iteration of the loop. If the condition is true, the code inside the loop is executed. If the condition is false, the loop terminates.
The iterator statement is executed after each iteration of the loop and is used to modify any variables used in the condition expression.
The following flowchart diagram represents a for loop.

C# for statement example
Here is an example of a for statement that loops through a block of code five times, and each time, it prints a string "The iteration number is" with the count value to the console. In this code, int i=1; is a declaration of a local variable that will participate throughout the loop.The i<=5; is the condition that decides how often iteration will continue.
public class Program {
public static void Main() {
for (int i = 1; i <= 5; i++) {
Console.WriteLine("The iteration number is " + i);
}
Console.ReadLine();
}
}
Output. The output of the above program looks like this

C# foreach statement
The foreach statement in C# is used to iterate over a collection of objects. It provides a simpler and more readable way to loop through the elements of a collection than using a for or while loop.
The general syntax of a foreach statement in C# is.
foreach (type variable in collection)
{
// code to be executed
}
In this syntax, type is the type of the elements in the collection, the variable is a variable that refers to each element in turn, and collection is the collection of objects being iterated over.
The following flowchart diagram represents a foreach loop.

C# foreach statement example
Here's an example of a foreach statement in C# that iterates over an array of strings and prints each element of the array. The string array daysofWeek has five elements.
public class Program {
public static void Main() {
string[] daysOfWeek = new string[] {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday"
};
foreach(string Day in daysOfWeek) {
Console.WriteLine("The Day is : {0}", Day);
}
Console.ReadLine();
}
}






Santhakumar MunuswamyPosted Oct 18, 2015, 2:40 AM
Good article
Mukesh KumarPosted Oct 16, 2015, 1:18 AM
Thanks to all
RakeshPosted Oct 16, 2015, 12:31 AM
Good share
Priyaranjan K SPosted Oct 15, 2015, 2:31 PM
Thanks for the share
Shakti SaxenaPosted Oct 15, 2015, 7:51 AM
Awesome
Nilesh JadavPosted Oct 15, 2015, 7:50 AM
Good one sir
Sujeet SumanPosted Oct 15, 2015, 7:42 AM
Nice Article................
Yaduveer SainiPosted Oct 15, 2015, 6:59 AM
Very Very Nice article mukesh.
Harshad PansuriyaPosted Oct 15, 2015, 6:11 AM
Nice one