Preface
I had read a lot on why to prefer recursion over iteration, but I still have not seen any question that explain when to prefer iteration over recursion.
Motivation
Certainly, I believe there is no best tools to solve everything.
For example, both spoon and fork are eating tools, but spoon is more suited for eating rice while fork is more suitable for eating noodles.
Obviously, I will be a fanatic if I say that fork(or spoon) is the best tool for eating.
So, it is the same as recursion or iteration, both of them are tools, they each have their own benefits and disadvantages.
However, after searching through Googles and StackOverflow, although I found a lot of example on why recursion is more readable than iteration, but I still had not found any example on when iteration is more readble than recursion.
Thus, I seek to find out situations where iteration will be more readble. Just like how the ancient human realize fork is better than spoon at handling noodles.
Expected answer
Therefore, I hope to get answers that meet the following requirement:
- Explain a situation on when will iteration be more readable than recursion
- Give an example of algorithm (such as Bubblesort etc.) in pseudocode to demonstrate that iteration is more readable than recursion in such case
Note
- Please note that I don't care about performance, what I care is the code readability.
- Therefore, you can use any imperative language, but not Haskell (or its derivatives) as they don't support loops.