is the act of repeating a process with the aim of approaching a desired result. The result of one iteration, mostly, goes as input to next iteration. It is repetition of block of statements in a computer program to carry out a task.
can be defined by two properties:
- It has a simple base case (or cases)
- Then it has a set of rules that reduce all other cases towards the base case
Comparing and contrasting both of them:
Iteration repeats until condition fails. Recursion calls itself until base condition is reached.
Iteration involves: initialization, condition, execution and updation. In recursive function, only terminate condition is specified.
Iteration makes code longer and recursion keeps it short.
Iteration is faster and recursion is slower due to overhead of stack maintenance.
For above mentioned reason recursion takes more memory that iteration.
Infinite recursion can crash the system. Infinite loop uses CPU cycles repeatedly.
Iterative approach is more efficient in terms of memory utilization and speed of execution. Don’t go for recursion for simple programs or programs which are not recursive in nature.