Both involve performing the same operation over and over, but when iterating you don't start the next operation until the previous has finished. Recursion involves starting the next operation within the previous. That means that the first operation doesn't finish until the second has finished, which doesn't finish until the third has finished, etc. Recursion is good for certain situations but because of the aforementioned situation you can stretch your system resources during a long running operation. Run this code as an example of each and the difference:
Code:
private void Form1_Load(object sender, EventArgs e)
{
// Iteration.
for (int i = 0; i < 5; i++)
{
MessageBox.Show("Start iteration: " + i.ToString());
MessageBox.Show("End iteration: " + i.ToString());
}
// Recursion.
this.DoSomething(0);
}
private void DoSomething(int i)
{
MessageBox.Show("Start recursion: " + i.ToString());
if (i < 4)
{
this.DoSomething(i + 1);
}
MessageBox.Show("End recursion: " + i.ToString());
}