DEV Community

Cover image for Iteration or Recursion?
Noor E Jannat Nafia
Noor E Jannat Nafia

Posted on

Iteration or Recursion?

One of the most common misconception or jeopardy arises when encountering Data Structure & Algorithms.

Iteration:
Iteration is the repetition of a process.

Recursion:
Recursion is the process of calling a certain function by itself.

The definitions might make them appear similar. However, there are several differences among them to differentiate them properly.

Iteration Recursion
Set of instructions is repeated. The function calls itself.
Iteration consists of initialization and conditions. Recursion consists of base case (termination condition) only.
Infinite iteration can occur when the condition never becomes false. Infinite recursion can occur when the base case(s) can never be reached.
Iteration uses a data structure called stack. Recursion does use stack.
Fast in execution. Slow in execution.
The codes of iteration are usually longer. The codes of recursion are usually shorter.

Top comments (0)