Recursion vs Iteration
Question
When is recursion often a better choice than a loop?
Hint
Think about problems that are defined in terms of themselves.
Explanation
Recursion shines when the problem has a self-similar structure — like traversing a tree, exploring a filesystem, or solving divide-and-conquer algorithms (merge sort, quick sort). For simple counting loops, iteration is usually clearer and more efficient. Recursion has overhead (call stack) and can cause stack overflow for deep recursion.
Related Resources
Related Lessons
- Recursion
Learn about recursive functions.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.