Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 8
    I don't agree with the premise of this answer. Readability of recursion vs iteration is a subjective matter. A technique one programmer finds most readable might not be the same for another imo. Commented Dec 16, 2018 at 1:55
  • The if statement in the iteration example is unnecessary and I believe will result in the same number of cycles either way. Interestingly, if I implement this in Rust (just because I kinda know it) and view the assembly, the while loop's false condition jumps to the if branch instead of duplicating the return code. If I remove the if statement, it places the return code right after while condition, which looks to be more efficient when factorial(n>1) and just as efficient when n<=1 (eg, the last call). rust.godbolt.org/z/q8Ak5c Commented Feb 12, 2019 at 20:39
  • if statement is not unnecessary as zero factorial is 1. 0!=1 if (num <= 1) return result; Commented Aug 15, 2019 at 16:19
  • Wait, are you saying iteration is always faster than recursion??? Then why am I learning recursion in school? SMH Commented Sep 4, 2019 at 3:00
  • 1
    @NoName in some other languages tail recursion is optimized, so, who knows, we might see optimizations in future Java versions as well Commented Jun 21, 2020 at 21:31