RECURSION
- Recursion - The process of calling a function by itself.
- Recursion uses selection structure.
- Infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on some condition.(base case)
- Recursion terminates when a base case is recognized.
- Recursion is usually slower than iteration due to the overhead of maintaining the stack.
- Recursion uses more memory than iteration.
- Infinite recursion can crash the system.
- Recursion makes code smaller.
ITERATION
- Iteration - these are loop-based repetitions of a process.
- Iteration uses repetition structure.
- An infinite loop occurs with iteration if the loop condition test never becomes false.
- Iteration terminates when the loop condition fails.
- Iteration does not use stack so it's faster than recursion.
- Iteration consumes less memory.
- Infinite uses CPU cycles repeatedly.
- Iteration makes the code longer.
Illustration of Recursion & Iteration:
Recursion Vs Iteration |
Remember any recursive problem can be solved iteratively. But you can't solve all problems using recursion.
No comments:
Post a Comment