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*
- 8I 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.Advait Saravade– Advait Saravade2018-12-16 01:55:27 +00:00Commented 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/q8Ak5cChinoto Vokro– Chinoto Vokro2019-02-12 20:39:39 +00:00Commented Feb 12, 2019 at 20:39
- if statement is not unnecessary as zero factorial is 1. 0!=1 if (num <= 1) return result;Jin Thakur– Jin Thakur2019-08-15 16:19:59 +00:00Commented Aug 15, 2019 at 16:19
- Wait, are you saying iteration is always faster than recursion??? Then why am I learning recursion in school? SMHNoName– NoName2019-09-04 03:00:56 +00:00Commented 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 wellhipokito– hipokito2020-06-21 21:31:16 +00:00Commented Jun 21, 2020 at 21:31
Add a comment |
How to Edit
- Correct minor typos or mistakes
- Clarify meaning without changing it
- Add related resources or links
- Always respect the author’s intent
- Don’t use edits to reply to the author
How to Format
- create code fences with backticks ` or tildes ~ ```
like so
``` - add language identifier to highlight code ```python
def function(foo):
print(foo)
``` - put returns between paragraphs
- for linebreak add 2 spaces at end
- _italic_ or **bold**
- indent code by 4 spaces
- backtick escapes
`like _so_` - quote by placing > at start of line
- to make links (use https whenever possible) <https://example.com>[example](https://example.com)<a href="https://example.com">example</a>
How to Tag
A tag is a keyword or label that categorizes your question with other, similar questions. Choose one or more (up to 5) tags that will help answerers to find and interpret your question.
- complete the sentence: my question is about...
- use tags that describe things or concepts that are essential, not incidental to your question
- favor using existing popular tags
- read the descriptions that appear below the tag
If your question is primarily about a topic for which you can't find a tag:
- combine multiple words into single-words with hyphens (e.g. python-3.x), up to a maximum of 35 characters
- creating new tags is a privilege; if you can't yet create a tag you need, then post this question without it, then ask the community to create it for you
lang-java