What is the difference between iteration
and recursion
and why/when is one better:
while (true) {
// Iterating
}
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.