DEV Community

loading...

Iteration Statements

Eduardo Julião
Software Developer | Geek since 1993 | @monsieurxuxu on twitter
・3 min read

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.

for(<initializer>; <condition>; <iterator>)
{
  // Code to be executed
}
Enter fullscreen mode Exit fullscreen mode

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.

for(int i = 0; i < 10; i++)
{
    Console.WriteLine("Value of i: {0}", i);
}
Enter fullscreen mode Exit fullscreen mode

Iterator removes a value of i each loop.

for(int i = 10; i > 10; i--)
{
    Console.WriteLine("Value of i: {0}", i);
}
Enter fullscreen mode Exit fullscreen mode

Initializer can be declared outside the for loop
In this scenario, the i variable can be accessed outise the loop

int i = 0;
for(; i < 10; i++)
{
    Console.WriteLine("Value of i: {0}", i);
}
Enter fullscreen mode Exit fullscreen mode

Iterator can be managed inside the loop

for(int i = 0; i < 10; )
{
    Console.WriteLine("Value of i: {0}", i);
    i++;
}
Enter fullscreen mode Exit fullscreen mode

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

int i = 0
for(; i < 10; )
{
    Console.WriteLine("Value of i: {0}", i);
    i++;
}
Enter fullscreen mode Exit fullscreen mode

Initializer, Condition and Iterator are managed ooutside the parenthesis

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

Working with arrays

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);
}
Enter fullscreen mode Exit fullscreen mode

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

int[] array = new [] { 1, 2, 3  };
Console.WriteLine(array[0]); // Outputs 1
Console.WriteLine(array[1]); // Outputs 2
Console.WriteLine(array[2]); // Outputs 3
Enter fullscreen mode Exit fullscreen mode

Foreach

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

foreach(<DataType> <variableName> in <collection)
{
  // code
} 
Enter fullscreen mode Exit fullscreen mode

Example

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);
}

Enter fullscreen mode Exit fullscreen mode

While

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

while(<condition>)
{
  // Code
}
Enter fullscreen mode Exit fullscreen mode

Example

Execute the instrunction until i is smaller than 1

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

Using a boolean variable

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.
}
Enter fullscreen mode Exit fullscreen mode

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.

do
{
  // Code
} while(<condition>)
Enter fullscreen mode Exit fullscreen mode

Examples

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

} while (i < 10);
Enter fullscreen mode Exit fullscreen mode

Discussion (0)