Iteration, For Loops, Foreach, While and Do

Iteration, For Loops, Foreach, While and Do

Iteration

Iteration is another fundamental programming concept and equally as important in C#. Iteration allows a statement, or collection of statements to run until some condition is met.

We will be talking about the For, Foreach, While and do concepts in this tutorial.

While Loops

A while loop will execute code in its associated code block until specified condition no longer evaluates to be true. The condition is tested before entering each loop.

You cannot change the evaluated value from the while statement, so this must be done from outside the loop or within the code block e.g. incrementing a value.


using System;

public class Program
{
	public static void Main()
	{
		int i = 5; //the identifier i is commonly used as an iterator value

		while (i < 10)
		{
			Console.WriteLine($"The value of i is {i}");
			i++;
		}
	}
}
                

If nothing is done to influence the evaluation then the while loop will run until the program is closed.


using System;

public class Program
{
	public static void Main()
	{
		int i = 5; //the identifier i is commonly used as an iterator value

		//while (i < 10)
		//{
		//	Console.WriteLine($"The value of i is {i}");
		//	i++;
		//}
	}
}
                

do-while

A do-while loop is very similar to a while loop, however, the code block is guaranteed to run at least once before the condition is tested. This means that the condition is now tested at the end of the loop.


using System;

public class Program
{
	public static void Main()
	{
		int i = 5;

		do
		{
			Console.WriteLine($"The value of i is {i}");
			i++;
		} while(i < 10);
	}
}
                

Please note the semicolon after the condition.

For Loops

For loops build on top of the logic for a while loop. For loops include the ability to declare and initialise an iterator as well as dictating what will happen to the iterator at the end of each loop.

A for loop iteration

For loop will still evaluate a condition before determining if the execution can enter the code block. The evaluation will happen before the start of the loop.


using System;

public class Program
{
	public static void Main()
	{
		int x = 20;

		for(var i = 0; i < x; i++)
		{
			Console.WriteLine($"{i} \t of \t {x}");
		}

	}
}
                

The For statement has 3 components. The initialization clause which declares and initialises variables, the condition clause which determines what condition must evaluate to true to run and the iteration clause which determines what happens to the iterator at the end of each loop.

Foreach Loops

The foreach loops are for loops that can iterate over any enumerable C# object. For now, all we need to know about an enumerable object is that they have elements that can be visited independently of each other.

As we learned earlier, a string is an ordered collection of characters and can be addressed with an index starting from 0.


using System;

public class Program
{
	public static void Main()
	{
		string x = "ARGGHHH";

		foreach(var c in x)
		{
			Console.WriteLine(c);
		}
	}
}
                

The two components in a foreach statement are the iteration variable and its type as well as the enumerable object we are looping over.

We can use an implicit variable (var) for the c in this case as the compiler will recognise that an element of the string will be a character.