This is a discussion on Recursion versus Looping in OOP within the Programming forums, part of the Tutorials category; Recursion versus Looping in OOP:- Any algorithm that can be coded with recursion can also be coded with a loop. ...
Recursion versus Looping in OOP:-
Any algorithm that can be coded with recursion can also be coded with a loop. Both approaches achieve repetition, but which is best to use?
There are several reasons not to use recursion. Recursive function calls are certainly less efficient than loops. Each time a function is called, the system incurs overhead that is not necessary with a loop. Also, in many cases, a solution using a loop is more evident than a recursive solution. In fact, the majority of repetitive programming tasks are best done with loops.
Some problems, however, are more easily solved with recursion than with a loop. For example, the mathematical definition of the GCD formula is well suited to a recursive approach. If a recursive solution is evident for a particular problem, and the recursive algorithm does not slow system performance an intolerable amount, then recursion would be a good design choice. If a problem is more easily solved with a loop, however, you should take that approach.