r/computerscience • • Oct 12 '26

General A good example of iteration vs recursion

The Song that Never Ends is iterative. It loops and is aware that it remains the same song.

The Song that Gets on Everybody's Nerves just sings about a song, and sings "this is how it goes:", calling itself.

21 Upvotes

9 comments sorted by

View all comments

0

u/ohkendruid Oct 12 '26

These are great!

I dunno about the "vs", though, because iteration and recursion have an awful lot in common.

I guess "iteration" suggests that the range of iteration is known up front, but it is only a week suggestion. For example, a C "for" loop is happy to go around a linked list, rubbing the body of the loop one time for each link. Such a loop is using the iteration construct of the language but is still effectively recursion.