Jitesh Byahut 9/3/2015 1919

Iteration Statements In C#

Iterative statement means loop that is used for doing some task again and again multiple times. For example using loop you can print number from 1 to 100 or any repeated task. There are different-different loops in C# used for different-different purpose. In C# followings are types of loops:

1. For Loop

2. Foreach Loop

3. While Loop

4. Do-While Loop

For loop

For loop is used for repeating a task to a fixed number of times. In this type of loop you know the number of iteration in advance. For-block contains three statements:

1. Initializer: Used for initializing a variable that run only first time when loop started. You can also use already initilized variable or calling a method or creating a new object.

2. Conditioner: Used for defining a condition for looping. Every time this will run and check condition is true or false. If condition is true then only next loop will run otherwise cursor jumps outside of for-block.

3. Iterator: Used for maintaining the loop every time and evaluating the condition. It can be increment or decrement.

Note: initializer, condition and iterator are optional.

// for loop syntax

//for(initializer; conditioner; iterator;)

//{

    // statement

//}

 

for (int x = 1; x < 5; x++)

{

    Console.WriteLine(x);

}

 

// Output:

// 1

// 2

// 3

// 4

In above example, we are using for loop for printing number 1 to 4. We initialize a variable x with 1 and incremented by 1 till x is less than 5.

Foreach Loop

Foreach loop used to iterate all the element from a collection one by one. Foreach loop is continue executing until all the elements are not fetched from collection. In this type of loop, you don't know the number of iteration in advance.

In foreach-block, we have to define a data-type object that is same as collection data-type for example defined a integer object if collection is integer type array or define a string object if collection is string type array. After that use in keyword that fetched one by one element from the collection and transferred in defined object. After that you can used the object according to your logic.

int[] items = new int[] { 1, 2, 3, 4, 5 };

 

foreach (int item in items)

{

    Console.WriteLine(item);

}

 

// Output:

// 1

// 2

// 3

// 4

// 5

 

string[] cities = new string[] { "Delhi", "Mumbai", "Patna", "Chennai", "Kolkata" };

 

foreach (string city in cities)

{

    Console.WriteLine(city);

}

 

// Output:

// Delhi

// Mumbai

// Patna

// Chennai

// Kolkata

In above examples we are using foreach loop. In first foreach loop we are retrieving one by one each element from integer type array. In second foreach loop we are retrieving one by one each element from string type array.

While Loop

While loop continuously executed until while condition is not false. When control start then first check condition is true or not. If condition is true then control enters into while-block and executing all statements inside the block. Again control moves to top and check condition is true or not and it executing continuously while statement until condition evaluated to false.

int i = 5;

 

while (i < 11)

{

    Console.WriteLine(i);

    i++;

}

 

// Output:

// 5

// 6

// 7

// 8

// 9

// 10

Do While Loop

In do-while loop first run all the statement inside do-block and after that it check the condition of while-block. Its continuously executing do-block until while condition is false.

int i = 5;

 

do

{

   Console.WriteLine(i);

   i++;

} while (i < 11);

 

// Output:

// 5

// 6

// 7

// 8

// 9

// 10

// 11



Please give your feedback for improving this page