Why would you use recursion over a basic loop?

I have just read that loops perform quicker than recursion and can also cause errors if it’s called too many times. I’m not understanding why you would use them over a loop.

Can anyone possibly explain why you would use recursion over a loop?

If you have a fixed number of iterations, like iterating the length of an array, then you should use a loop.

If you have an arbitrarily nested structure, then you likely want to use recursion.

“Arbitrarily nested structure”

I’m a little thrown off here. Could you possibly give a basic example?

An “arbitrarily nested structure” is not very “basic”.

Imagine a family tree. Each set of parents can have an arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children, each with their own arbitrary number of children…

I’d use recursion to count the number of people in the family.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.