Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends i was reading article "Iterative vs. Recursive Approaches" i found that iterative process is more reliable in this scenarion.

Can some one say me when can we use iteration over recurssion.

Thank you.

What I have tried:

The given example shown is shows efficient results for iterative appraoch.i have tried few of other example but not it also shows the same result.
Posted
Updated 29-Feb-16 22:38pm
Comments
PIEBALDconsult 29-Feb-16 23:08pm    
Never.

In the majority of cases iteration is by far more efficient (in terms of execution time and memory resources) than recursion. So the simple rule is:

If you can solve a problem by iteration without making the code overly complex, then stick to iteration.

The reason for iteration being generally more efficient is that in general the cost of a loop construct is lower than a function call. The latter requires storing/loading several registers, including the program counter and in some cases doing some routine work like setting up a context for exception processing. That all leads to more CPU-cycles and to more stack usage than a simple loop would take.

On the other hand, if you know that your recursion is only going to go to a limited nesting level and execution time is not a major issue, you might prefer writing your code as a recursive function, if that makes the code more readable and better maintainable.

Typical examples in which recursion is generally the better choice are: Syntax analysis in a parser, certain numerical algorithms with a lot of complexity, or in general algorithms that are recursive in nature and inherently complex.

If in doubt and performance is really critical, do both implementations and compare their run-time behavior.
 
Share this answer
 
v2
Comments
Philippe Mori 3-Mar-16 21:33pm    
At the end of first sentence you said iteration instead of recursion...
nv3 4-Mar-16 2:50am    
Thanks Philippe!
Is the article you found ? :Iterative vs. Recursive Approaches[^]
Here is some reading :
http://www.cs.cornell.edu/info/courses/spring-98/cs211/lecturenotes/07-recursion.pdf[^]
Fibonacci number - Wikipedia, the free encyclopedia[^]
Iteration vs. Recursion in Java[^]
There not a single answer.
My experience, recursion is usually easier to write because it is closer to math definition, iteration generally look more complicated but also more efficient.

Mustafa_ub2016 wrote:
Can some one say me when can we use iteration over recursion.
Almost every times for usual problems. There is only a few problems where iteration is getting really complicated.
When writing a compiler, recursion is commonly used because it is a complex system that need to call itself from multiple places.
 
Share this answer
 
v3
Comments
Mustafa_ub2016 29-Feb-16 23:36pm    
Thank you for your reply and time..Appreciate.
Sergey Alexandrovich Kryukov 1-Mar-16 1:39am    
Well, your statements are questionable. Yes, the ease of recursion is a good point, but the beginners have more problems with recursion than iterations, because recursion requires understanding they lack. Correct answer would be: it depends; estimate or measure typical resources and time used, worse bad best-case scenario, take into account priorities...

Why did you mention Fibonacci? You did not tell us your opinion on which algorithm is better. It's a typical example when recursion would be really bad, for any serious length or a row...

—SA
Patrice T 1-Mar-16 4:19am    
Hi Sergey,
The link to Fibonacci is a mistake (I forgot to remove it after I changed my draft) :-)
I prefer iteration is most cases because recursion is going bad really too fast.
In my answer, I mainly gave links to more stuff.
Fill free to coin your own answer, I am sure it will be interesting as usual.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900