What is the difference between iteration
and recursion
and why/when is one better:
while (true) {
// Iterating
}
They are different ways to do the same thing. All recursive implementations can be done with a (or multiple) loop(s) and vise versa. It is more about the logic behind it, a way to think about it. Factorial, although not the best example, is n * (n-1)!
so it makes sense to use it recursively.
There are two main differences between Recursion and an Iterative Version of the same algorithm.
First of all, some times it is almost better to understand a recursive algorithm than an iterative one (At least if you are experienced programmer) So it does increase expressivity and in some cases readability (It might also lead to the exact opposite in other cases)
Expresivity is a huge deal on programming languages and be able to write the same code in 5 lines instead of 20 is a huge deal.
On the downside, it decreases the performance of your code. Recursive functions have to keep the function records in memory and jump from one memory address to another to be invoked to pass parameters and return values. That makes them very bad performance wise.
Sum Up:
Iterative Algorithms = Fast Performance but hard to write (sometimes hard to read too)
Recursive Algorithms = Fast to write but Bad performance wise (Sometimes easier to understand too)
Take this example:
public static long fib(long n) {
if (n <= 1) return n;
else return fib(n-1) + fib(n-2);
}
vs
if ((n == 1) || (n == 2)) {
return 1;
} else {
long prev = 1, current = 1, next = 0;
for (long i = 3; i <= n; i++) {
next = prev + current;
prev = current;
current = next;
}
return next;
}
Source:
http://www.csd.uwo.ca/Courses/CS1027a/code/FibonacciDemo.java
In theory, you can always swap between iteration and recursion. However, at least in case of C/C++/C#/Java, the compiler offers you some support which might make the solution more elegant, especially when you don't know the number of loops before.
The best example is listing all the files inside a folder and it's descendants. If there are several subfolders containg subfolders, normaly in iteration mode you need a stack to save all folders you need to analyze. In case of recursive approach, the stack is already provided by the compiler, and the solution is more elegant.
They can be used interchangeable to solve different problems. In essence you can write recursive functions iteratively and vise versa.
Iteration may increase the performance of your program. Whereas recursion may give a more intuitive and elegant result. You can choose either which to your preference!
The main difference between recursion and iteration is memory usage.
For every recursive call needs space on the stack frame resulting in memory overhead.
Let me give you an example. Imagine in one case you forgot to write the base case for your recursive function resulting in endless recursive calls and in other case you wrote an infinite loop.
Since every recursive function assigns new memory space, in first case your code will give a stack overflow exception but in second case it will keep running forever.
So it better to make your iterative code more understandable than using Recursion.
Recursion
Iteration
Data taken from here.