Recursion and Iteration in C (Comparison & Difference)
The purpose of Recursion and Iteration is to execute a set of instructions repeatedly until some condition is satisfied. To achieve this recursion uses self-function call (Example) while iteration uses looping statement.
In this article we explain key similarities, differences and comparision between recursion and iteration point wise:
- Both Iteration And Recursion Are Based On Control Statements: Iteration uses a repetition statement (such as for, while, do-while); Recursion uses a selection statement such as
(if, if-else, or switch).
- Both Iteration And Recursion Involve Repetition: Iteration explicitly uses a repetition statement; Recursion achieves repetition through repeated function calls.
- Both Iteration And Recursion Involve A Termination Test: Iteration terminates when the loop continuation condition fails; Recursion terminates when a base case is recognized.
- Both Iteration And Recursion Can Occur Infinitely: An infinite loop occurs in the iteration if the loop continuation test never becomes false (e.g.
for(i=1; i>0; i++)); Infinite recursion occurs if the recursion step does not reduce the problem each time in a manner that converges on the base case.