Recursion vs loops

recursion vs loops python
recursion vs loops performance
recursion vs loop time complexity
recursion vs loops c++
recursion vs iteration
recursion vs iteration python
tail recursion
recursion vs while loop performance

I'm facing a problem where both recursion and using a loop seem like natural solutions. Is there a convention or "preferred method" for cases like this? (Obviously it is not quite as simple as below)

Item Search(string desired, Scope scope) {
    foreach(Item item in scope.items)
        if( == desired)
            return item;

    return scope.Parent ? Search(desired, scope.Parent) : null;
Item Search(string desired, Scope scope) {
    for(Scope cur = scope; cur != null; cur = cur.Parent)
        foreach(Item item in cur.items)
            if( == desired)
                return item;

    return null;

Recursion vs loops, One of the big differences between recursion and looping is the way that a recursive function terminates. In the above example, a for loop ends at the end of the sequence it is looping over. However, a recursive function could continue indefinitely since it doesn't necessarily have a sequence of data. Difference Between Recursion and Loop Definition. Recursion is a method of calling a function within the same function. In contrast, loop is a control Speed. Speed is a major difference between recursion and loop. Recursion execution is slower. However, loop executes Stack. In recursion, the

If performance matters, then benchmark both and choose on a rational basis. If not, then choose based on complexity, with concern for possible stack overflow.

There is a guideline from the classic book The Elements of Programming Style (by Kernighan and Plauger) that algorithm should follow data structure. That is, recursive structures are often processed more clearly with recursive algorithms.

Recursion vs. Looping in Python, in its section on approaching technical interview problems using recursion. Solving a Python problem iteratively might include using a for or� Recursion has more expressive power than iterative looping constructs. I say this because a while loop is equivalent to a tail recursive function and recursive functions need not be tail recursive.

Recursion is used to express an algorithm that is naturally recursive in a form that is more easily understandable. A "naturally recursive" algorithm is one where the answer is built from the answers to smaller sub-problems which are in turn built from the answers to yet smaller sub-problems, etc. For example, computing a factorial.

In a programming language that is not functional, an iterative approach is nearly always faster and more efficient than a recursive approach, so the reason to use recursion is clarity, not speed. If a recursive implementation ends up being less clear than an iterative implementation, then by all means avoid it.

In this particular case, I would judge the iterative implementation to be clearer.

When to Loop? When to Recurse?. How to make the most of , Is this statement right or proven at all? If you maintain your own stack it would be correct. Otherwise recursion can do things loops can't, like walk a tree. Loops� Generally recursion probably doesn't beat looping often or ever on problems of decent size if both are using really good algorithms(not counting implementation difficulty) , it may be different if used with a language w/ tail call recursion(and a tail recursive algorithm and with loops also as part of the language)-which would probably have very similar and possibly even prefer recursion some of the time.

If you are using a functional language (doesn't appear to be so), go with recursion. If not, the loop will probably be better understood by anyone else working on the project. Of course, some tasks (like recursively searching a directory) are better suited to recursion than others.

Also, if the code cannot be optimized for tail end recursion, the loop is safer.

Recursion Vs Loop, Recursion VS Loops 080916 according to this , there is nothing that can be done only be Duration: 2:14 Posted: 9 Aug 2016 Using recursion, you're incurring the cost of a function call with each "iteration", whereas with a loop, the only thing you usually pay is an increment/decrement. So, if the code for the loop isn't much more complicated than the code for the recursive solution, loop will usually be superior to recursion.

Use the loop. It's easier to read and understand (reading code is always a lot harder than writing it), and is generally a lot faster.

Recursion VS Loops 080916, Recursion is not intrinsically better or worse than loops - each has advantages and disadvantages, and those even depend on the programming language (and � The process in which a function calls itself is called recursion. Recursion is a method where the solution of a problem depends upon the solutions that are

Is recursion faster than loops?, In simple terms, an iterative function is one that loops to repeat some part of the The difficulty, when teaching or learning about recursion, is finding examples� As your recursion goes deeper the amount of memory increases. In contrast the for loop you wrote just uses the fixed amount of memory provided by one stack frame push.

Recursion and Iteration | Programming, This is a blog comparing the performance of recursion with looping technique for the same algorithm. Technically, iterative loops fit typical computer systems better at the hardware level: at the machine code level, a loop is just a test and a conditional jump, whereas recursion (implemented naively) involves pushing a stack frame, jumping, returning, and popping back from the stack.

Performance Of Recursion Vs Loop Using JavaScript, Is this statement right or proven at all? <i>If you maintain your own stack it would be correct. Otherwise recursion can do things loops can't, like walk a tree.</i>.

  • The recursive solution doesn't seem to be correct. "scope.Parent" doesn't yield a boolean expression, and based on your iterative solution it looks like you meant "scope.Parent != null". Is that right?
  • Right. I was trying to keep things concise with an implicit conversion to boolean.
  • possible duplicate of Recursion or iteration?
  • As at least one answer pointed out, recursion and iteration are mutually translatable in theory. Languages and compilers differ in both the semantics and performance characteristics of various constructs. Language communities furthermore differ in their stylistic preferences. As stated, this question is, at best, primarily opinion-based.
  • Maybe add an additional point about tail recursion (I know it doesn't apply in Java) in the case where stack depth is a concern?
  • In a loop, each iteration is a 'refreshed' scope, all of the variables from the previous iteration are deleted (variables for iteration i-1 can't be accessed from iteration i, because the variables from i-1 are deleted on the transition from iteration i-1 to iteration i). In a recursive function, each new iteration is run in a subscope of the last iteration (the variables for iteration i-1 are still alive during iteration 1, they just exist in a different scope). This describes procedural languages, but some functional languages actually do optimizations that make recursion more effecient
  • certain problems cannot be solved by iterations easily…
  • There seems to be an issue here with the loop because you are modifying the collection actualSubFolder within the for loop.
  • certain problems cannot be solved by iterations…
  • If you need the comments for that version but not for the loop, than I would argue that is a sign that the loop should be preferred in this case.
  • No, I personally think the loop needs even more comments. The comparison of readability is just much easier if you take comments into account. For the average-lazy-dontwanttothinkaboutit fellow programmer, both versions are not "self explanatory" enough.
  • If what's make the recursion to be more readable is comments, then we missed the whole beauty and simplisity of recursion...