Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Recursion and Iteration?
New Posts  All Forums:Forum Nav:

# Recursion and Iteration?

I'm reading and watching some stuff posted on MIT about SICP (Structure and Interpretation of Computer Programs) and i tried googling this up but it seems like everyone has a different answer (or so i think) so im asking you guys this.

whats the difference between iteration and recursion?
 Mein Komp (14 items)
 Mein Komp (14 items)
To define recursion you must first understand recursion...haha, old computer science joke my teacher told us.

Iteration is how it sounds, iterating through the data and performing calculations or manipulations:

Code:
``````public int IterativeFactorial(int fact)
{

int total;

if (fact <= 1)
{
return 1;
}
else
{
for(int i = 1; i < fact; i++)
{
total *= fact;
}
}

}``````
Recursion is a function that calls itself:

Code:
``````public int RecursiveFactorial(int fact)
{
if (fact <= 1)
{
return 1;
}
else
{
return fact * RecursiveFactorial(fact - 1);
}
}``````
Notice how in this example of a factorial calculation, the function ends up calling itself if the number is greater than 1. In the iterative example, we iterate through all the data, and total up the factorial.

Understand?

Edit: Sorry if my iterative algorithm is wrong, I wrote this up quickly, and don't really have time to check for correctness
Edited by CovertCover - 12/31/10 at 6:39pm
 My System (13 items)
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion
Case
HAF 932
 My System (13 items)
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion
Case
HAF 932
yah, thx
 Mein Komp (14 items)
 Mein Komp (14 items)
New Posts  All Forums:Forum Nav:
Return Home
Back to Forum: Coding and Programming
• Recursion and Iteration?
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Recursion and Iteration?