Skip to main content

What is the difference between recursion and Iteration?

· 4 min read
Stacey "Elugens" Wilson

Both Recursion and Iteration execute the sequence of instructions repeatedly. When a statement in a function calls itself again, this is called recursion, and Iteration occurs when a loop executes the body repeatedly until the controlling condition becomes false. The critical distinction between recursion and Iteration is that recursion is a procedure always applied to a function. In contrast, We can apply Iteration to a sequence of instructions that we wish to run again.

Understanding the Recursive Structure

  • We use a selection structure in recursion.
  • If the recursion step does not reduce the issue in a way that converges on some condition (base case), infinite recursion occurs, and infinite recursion might crash the system.
  • When a base case is detected, the recursion ends.
  • Because of the complexity of maintaining the stack, recursion is often slower than Iteration.
  • Iteration consumes less memory than recursion.
  • Recursion reduces the size of the code.

Recursive JavaScript Approach:

function pow(x, n) {
return n == 1 ? x : x * pow(x, n - 1);
}

console.log(pow(2, 3)); // logs 8


Understanding the Iterative Structure

  • If the loop condition test never returns false, iteration results in an infinite loop, and infinite looping wastes CPU cycles continually.
  • The Iteration stops when the loop condition fails.
  • Because Iteration does not require the stack, it is faster than recursion.
  • Iteration consumes less RAM than recursion.
  • Iteration makes the code longer.

Iterative JavaScript Approach:

/*Iterative Function to calculate (w^y) in O(logy)*/
function power(w, y) {
// Initialize result
let res = 1;

while (y > 0) {
// If y is odd, multiply x with result
if (y & 1) res = res * w;

// y must be even now
y = y >> 1; // y = y/2
w = w * w; // Change w to w^2
}
return res;
}

// Driver program to test the above functions

let w = 2;
y = 3;

console.log(`${w} to the power of ${y} =` + power(w, y));

// expected output: 8

Simple Non-Iterative Approach: Exponentiation


function pow(x, n) {
if (n === 1) {
return x;
} else {
x = x ** n;
}

return x;
}

console.log(pow(2, 3)); // 8

Which is preferable, Iteration or recursion?

  • It is sometimes more challenging to determine the temporal complexity of recursive code than iterative code.
  • When compared to Iteration, recursion has a significant overhead. It is frequently slower since all function calls must be placed in a stack to allow the caller functions to return, and Iteration does not have this overhead.
  • Endless recursive calls can cause a CPU to crash because infinite recursive calls can occur due to a mistake in the base condition, which never becomes false and keeps calling the function, resulting in a system CPU crash.
  • Infinite Iteration because of a mistake in iterator assignment or incrementor, or in the termination condition, results in infinite loops, which may or may not result in system faults but ceases program execution.
  • We can characterize every recursive call as a loop, which is what the CPU eventually performs. And more explicitly, recursion entails stacking the function calls and scopes. However, transforming your recursive approach to a looping one may require much work and make your code less manageable.

Conclusion

The difference between recursion and Iteration is that recursion is simply a function call. The function is being called by itself until a specific condition gets met, while Iteration is when a loop gets repeatedly executed until a specific condition gets met. A recursive solution is usually shorter than an iterative one.