Q. Compare Iteration and Recursion.
Recursion is when a statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes until the controlling condition becomes false. The primary difference between recursion and iteration is that is a recursion is a process, always applied to a function.
The primary difference between recursion and iteration is that is a recursion is a process, always applied to a function. The iteration is applied to the set of instructions which we want to get repeatedly executed.
Key Differences Between Recursion and Iteration
- Recursion is when a method in a program repeatedly calls itself whereas, iteration is when a set of instructions in a program are repeatedly executed.
- A recursive method contains a set of instructions, statement calling itself, and a termination condition whereas iteration statements contain initialization, increment, condition, set of instruction within a loop and a control variable.
- A conditional statement decides the termination of recursion and control variable’s value decide the termination of the iteration statement.
- If the method does not lead to the termination condition it enters to infinite recursion. On the other hand, if the control variable never leads to the termination value the iteration statement iterates infinitely.
- Infinite recursion can lead to system crash whereas, infinite iteration consumes CPU cycles.
- Recursion is always applied to method whereas, iteration is applied to a set of instruction.
- Variables created during recursion are stored on stack whereas, iteration doesn’t require a stack.
- Recursion causes the overhead of repeated function calling whereas, iteration does not have a function calling overhead.
- Due to the function calling overhead execution of recursion is slower whereas, execution of iteration is faster.
- Recursion reduces the size of code whereas, iterations make a code longer.
|BASIS FOR COMPARISON||RECURSION||ITERATION|
|Basic||The statement in a body of function calls the function itself.||Allows the set of instructions to be repeatedly executed.|
|Format||In recursive function, only termination condition (base case) is specified.||Iteration includes initialization, condition, execution of statement within loop and update (increments and decrements) the control variable.|
|Termination||A conditional statement is included in the body of the function to force the function to return without recursion call being executed.||The iteration statement is repeatedly executed until a certain condition is reached.|
|Condition||If the function does not converge to some condition called (base case), it leads to infinite recursion.||If the control condition in the iteration statement never become false, it leads to infinite iteration.|
|Infinite Repetition||Infinite recursion can crash the system.||Infinite loop uses CPU cycles repeatedly.|
|Applied||Recursion is always applied to functions.||Iteration is applied to iteration statements or "loops".|
|Stack||The stack is used to store the set of new local variables and parameters each time the function is called.||Does not uses stack.|
|Overhead||Recursion possesses the overhead of repeated function calls.||No overhead of repeated function call.|
|Speed||Slow in execution.||Fast in execution.|
|Size of Code||Recursion reduces the size of the code.||Iteration makes the code longer.|