# why do we prefer recursion than while/for loops in writing C programmes??

Relevance

We don't always prefer recursion to while/for loops. We prefer recusion when it fits the model of the problem we are trying to solve. Problems that can be easily broken down into a repeated sub-problem are often more efficiently/easily written through recursion. Other problems are better suited for iterative (loops) solutions.

Have a look at my source for a more clear explanation.

• Joe
Lv 4

When calculating the time complexity of an algorithm, recursion is usually faster than loops. What actually happens is that you trade memory space (recursive functions push into the stack) for time. If you have heard of Big O notation, it will explain this a lot better. For example, A linear bubble sort, which is just a doubly nested loop, has a time complexity of O(N^2), where a merge sort is O(N*log N).

Sometimes it takes less code using recursion than loops also. However, there are cases where loops are better than recusion. It really depends on the problem.