Iteration Statements
Tags: csharp, beginners
Published: 2021-07-09 16:45:37


Iteration statements are code blocks that executes instructions in a loop.

There a 4 types of iteration statements

* For
* Foreach
* While
* Do... While

## For

The `for` loop keeps executing until a certain condition is met.

```csharp
for(<initializer>; <condition>; <iterator>)
{
  // Code to be executed
}
```

###  Initializer

Creates and initialize a variable to be used inside the `for` loop. This variable cannot be accessed from outside.

### Condition

A boolean expression that must return either true or false. The loop will keep executing until this expression returns `false`.

### Iterator

Defines the incremental or decremental of Initializer variable.

### Example

Iterator adds a value to i each loop.

```csharp
for(int i = 0; i < 10; i++)
{
    Console.WriteLine("Value of i: {0}", i);
}
```

Iterator removes a value of `i` each loop.

```csharp
for(int i = 10; i > 10; i--)
{
    Console.WriteLine("Value of i: {0}", i);
}
```

Initializer can be declared outside the for loop
In this scenario, the `i` variable can be accessed outise the loop
```csharp
int i = 0;
for(; i < 10; i++)
{
    Console.WriteLine("Value of i: {0}", i);
}
```

Iterator can be managed inside the loop

```csharp
for(int i = 0; i < 10; )
{
    Console.WriteLine("Value of i: {0}", i);
    i++;
}
```

Iterator managed insithe the loop and initializer variable declared outside the for loop

```csharp
int i = 0
for(; i < 10; )
{
    Console.WriteLine("Value of i: {0}", i);
    i++;
}
```

Initializer, Condition and Iterator are managed ooutside the parenthesis 

```csharp
int i = 0;
for(;;)
{
  if(i < 10)
  {
    Console.WriteLine("Value of i: {0}", i);
    i++;
  }
  else
    break; // force exit from loop
}
```

Working with arrays

```csharp
int[] array = new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

for(int i = 0; i < array.Count - 1; i ++)
{
  int currentItem = array[i];
  Console.WriteLine("Current value of item inside array is: " + currentItem);
}
```

In the example above we're using the `.Count` property of the array to know how many items are inside it.
Since arrays index start at 0, we use remove one iteration with `- 1`, without it, the for looop will look for an item that does not exists in the array and trhow an error.

To better exemplify this

```csharp
int[] array = new [] { 1, 2, 3  };
Console.WriteLine(array[0]); // Outputs 1
Console.WriteLine(array[1]); // Outputs 2
Console.WriteLine(array[2]); // Outputs 3
```

## Foreach

The `foreach` loop iterates through each item inside a collection.
The `DataType` must match the type of the collection you're iterating.

```csharp
foreach(<DataType> <variableName> in <collection)
{
  // code
} 
```

### Example

```csharp
int[] array = new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

foreach(int i in array)
{
  Console.WriteLine("Current value of i is: " + i);
}

```

## While

In the `while` loop, the code block will run until the condition returns true.

```csharp
while(<condition>)
{
  // Code
}
```

### Example

Execute the instrunction until `i` is smaller than 1

```csharp
int i = 0; // initializer
while(i < 10) // condition
{
  Console.WriteLine("Value of i: " + i);
  i++; // iterator
}
```

Using a boolean variable

```csharp
int i = 0;
bool isBiggerThan10 = false;
while(isBiggerThan10)
{
  Console.WriteLine("Value of i: " + i);
  i++; // iterator
  isBiggerThan10 = i > 10; // Checks if i is bigger than 10 and assing the result to the variable.
}
```

## Do... While

The difference between `while` and `do...while`, is that `while` checks the condition before executing, and `do...while` always executes at least once.

```csharp
do
{
  // Code
} while(<condition>)
```

### Examples

``` csharp
int i = 0;
do
{
    Console.WriteLine("i = {0}", i);
    i++;

} while (i < 10);
```