Learning recursive and iterative

Hi, can anyone explain the difference between recursive and iterative in layman's terms? I am reading and rereading and analyzing but I still don't understand!

Thanks
No one?
Iteration is repeating an operation, process, or procedure for some defined number of times until some condition has been met:

1
2
3
4
for (int i = 0; i < 100; i ++)
{ 
     std::cout << "Yeah Man!/n" ;
} 


Recursion is the a process repeating itself for a condition:

1
2
3
4
int recursion (int count)
{ if (count == 0) return 1;
   else return count * recursion (count - 1);
}


This happens to return the factorial of count. . .
Last edited on
Iteration has nothing to do with knowing how many times you need to iterate.
Well, until some condition has been met may be the better way to put it.
Recursive:
relating to or involving the repeated application of a rule, definition, or procedure to successive results.


Meaning that the next result requires that the rule be applied to previous results. For example, the Fibonacci sequence ( Fn = Fn-1 + Fn-2 ).

Iterative:
relating to or involving iteration, especially of a mathematical or computational process.


Iteration
the repetition of a process or utterance


Meaning to apply the same process over and over again.

They aren't mutually exclusive words, but they are a little more specific in computer science. A recursive function is a function that calls itself. An iterative function simply isn't recursive. I could be wrong about this, but the only time I've heard someone being specific about an "iterative" solution is one where they are specifically saying "do not use the recursive one".

Furthermore, it would not be wrong to say something along the lines of "the fourth iteration of the recursive function". We would be talking about the fourth time that the function had called itself.

Just to repost PCrumley48's example, here is a recursive factorial:
1
2
3
4
5
6
int factorial( int value )
{
  if ( value == 0 ) return 1;

  return value * factorial( value - 1);
}


And here is the iterative solution:
1
2
3
4
5
6
7
8
int factorial( int value )
{
  int result = 1;
  for ( int i = 2; i <= value; ++i )
    result *= i;

  return result;
}


Edit: Iteration is mostly described to apply something on every value of a container:
1
2
3
4
5
6
// declare an array
int array[] = { 1, 2, 3, 4, 5 };

// iterate through the array, multiplying each value by 2
for ( int i = 0; i < 5; ++i ) // forgive the magic number
  array[i] *= 2;
Last edited on
My son found this definition in an on-line geeks' dictionary:

recursion, n, see recursion

Topic archived. No new replies allowed.