Question:
In C language what is difference between looping & recursion? Which one is best?
Guneet K
2009-01-03 06:03:28 UTC
In C language what is difference between looping & recursion? Which one is best?
Seven answers:
padge61
2009-01-03 06:18:49 UTC
Looping versus recursion is not specific to the C language.



It is said that any problem which can be solved by recursion can be solved by iteration (looping). It is true, but sometimes the iterative way to do something is much more complex to set up and implement.



Consider the following problem:

You want to get the total size of a directory -- that is, the size of all the files in it and the size of all the files in its subdirectories, and that subdirectories' subdirectories, etc.



You could write a function that gets the directory listing, adds up the the file sizes, then goes through all the subdirectories calling itself on THOSE directories, and return the sum, to be added to the total in the calling function. What makes this recursive is that the function is calling itself. This is sometimes easier to think of, but often will use more memory.



Alternately, a function can add the file sizes, then add each directory to a list of further jobs needing to be done before the process is complete. Then, if there are any items in the job, it must switch to that directory and do the same. This would be the iterative way to do it.



But like all questions of which is better, it's an engineering discretion problem. If you have 15 minutes to write the program, and 4 gigs of ram, then by all means, use the recursive way. If you have all the time you need, but you must run in a very restricted environment, perhaps the iterative way is the best.
whitekt64
2009-01-03 11:19:31 UTC
Recursion is simply a loop with a stack. This is true for C as well as other languages.



Which approach you choose to use depends a lot on how easy it is to understand the solution when it is written as a loop vs. written as a stack.



There are also questions of algorithmic efficiency that come into play. If the recursive solution introduces inefficiencies that the looping solution would otherwise not introduce, then go with looping.



For example, Fibonacci numbers: Computing fib(n) recursively as fib(n-2) + fib(n-1) is very inefficient, since the same set of calculations used to compute fib(n-2) will be run for both calls, and the inefficiencies are compounded with each further recursive call. In that case, a loop is preferable to this recursive solution.
Daniel B
2009-01-03 06:15:07 UTC
Looping is simply executing one piece of code over and over, recursion also loops though the same code but in recursion a subroutine actually calls itself.



Neither can be defined as "best", it just depends on the problem you are trying to solve. Recursion is not used anywhere near as often as looping especially since it's harder to use properly. If recursive subroutines do not have proper exit conditions they can get caught in an infinite loop and eventually run out of memory.
anonymous
2009-01-03 06:15:10 UTC
Looping is best, because all problems are the same.



If you're running apples on a conveyer belt, you use looping. If you're picking apples off a tree, you use recursion.



Recursive apple picking: Try the left branch then the right branch, pick the apples.



Whenever a problem can be broken into multiple identical subproblems, that's when it makes sense to go recursive. Language is parsed recursively.



Loops just do the same thing, in a straight line, no branching.
awsom
2015-12-29 08:41:23 UTC
plz seprate by loop and recursion?tell me the best answer..
anonymous
2009-01-03 06:20:57 UTC
Why do people often ask 'what is best'??



If I ask: "I have a hammer and a screwdriver, which is best?", does that illustrate why your question is, well, ridiculous?
XILOS
2009-01-03 06:10:17 UTC
you can use www.wikipedia.com to answer ur question


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...