Tuesday 13 September 2016

Understanding the basic difference between Recursion & Iteration.

RECURSION

  1. Recursion - The process of calling a function by itself.
  2. Recursion uses selection structure.
  3. Infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on some condition.(base case)
  4. Recursion terminates when a base case is recognized.
  5. Recursion is usually slower than iteration due to the overhead of maintaining the stack.
  6. Recursion uses more memory than iteration.
  7. Infinite recursion can crash the system.
  8. Recursion makes code smaller.

ITERATION

  1. Iteration - these are loop-based repetitions of a process.
  2. Iteration uses repetition structure.
  3. An infinite loop occurs with iteration if the loop condition test never becomes false.
  4. Iteration terminates when the loop condition fails.
  5. Iteration does not use stack so it's faster than recursion.
  6. Iteration consumes less memory.
  7. Infinite uses CPU cycles repeatedly.
  8. Iteration makes the code longer.

Illustration of Recursion & Iteration:


Recursion Vs Iteration
Remember any recursive problem can be solved iteratively. But you can't solve all problems using recursion.

No comments:

Post a Comment