Difference Between Recursion and Iteration

Computer ProgrammingProgrammingMiscellaneous

In this post, we will understand the difference between recursion and iteration.

Recursion

  • It uses the selection structure.

  • Infinite recursion occurs if the step in recursion doesn’t reduce the problem to a smaller problem.

  • It also becomes infinite recursion if it doesn’t convert on a specific condition.

  • This specific condition is known as the base case.

  • The system crashes when infinite recursion is encountered.

  • It terminates when the base case is met.

  • It is slower than iteration since it has the overhead of maintaining and updating the stack.

  • It uses more memory in comparison to iteration.

  • Recursion reduces the size of the code.

Iteration

  • It uses the repetition structure.

  • It increases the size of the code.

  • It uses less memory in comparison to recursion.

  • It terminates when the condition in the loop fails.

  • An infinite loop occurs when the condition in the loop doesn’t become False ever.

  • It uses the CPU cycles again and again when an infinite loop occurs.

  • It doesn’t utilize the stack.

  • It is quick in comparison to recursion.

raja
Published on 24-Mar-2021 13:23:51
Advertisements