Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud
by Paul J. Deitel, Harvey M. Deitel
11.5 Recursion vs. Iteration
We’ve studied functions factorial and fibonacci, which can be implemented either recursively or iteratively. In this section, we compare the two approaches and discuss why you might choose one approach over the other in a particular situation.
Both iteration and recursion are based on a control statement: Iteration uses an iteration statement (e.g., for or while), whereas recursion uses a selection statement (e.g., if or if…else or if…elif…else):
Both iteration and recursion involve iteration: Iteration explicitly uses an iteration statement, whereas recursion achieves iteration through repeated function calls.
Iteration and recursion each involve a termination test: Iteration terminates when the loop-continuation ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access