This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

iteration vs. recursion  RSS feed

 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taking the lazy route here (haven't looked these up anywhere)...
Can someone give me a quick distinction between these two?
Thanks,
Pauline
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In functional programming recursion is just THE power.
In oldfashioned (fortran) programming recursion is replaced
by whichever means.
In Java I would like to use recursion, but I do not remember where, but here at the Javaranch recursion was not a favourite.
If this statement is wrong, please clarify for me too.
Peter
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Off the top of my head.
Iteration is the process of working on a problem repeatedly step by step, taking the result and then continuing to process further if necessary.
Recursions is the process of breaking a problem into smaller pieces and then working on the smaller pieces at the sametime until a base solution/case is reached. This general involves a section, function, method ... returning calls to its self. In non-structured languages like COBOL recursion is a bad thing to do; whereas, in structured languages it is acceptable.

This is the best I can do off the top of my head. I have to go back and read the books evertime I'm faced with this, I just hava a general idea of how it works. I've written only one programm that used recusion to solve a problem and that was a required class assignment.
So this isn't the most accurate explanation, but it is the best I can do without a book.

[This message has been edited by Richard Boren (edited June 14, 2001).]
 
Sheriff
Posts: 9087
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A recursive method is a method that calls itself either directly or indirectly.

Any problem that can be solved recursively can also be solved iteratively (nonrecursively).

Recursion is a powerful tool and is usually one of the easiest places to create bugs and difficult for newbies to understand. While pulling off a good recursion always has a high cool factor, it is generally best to avoid. There are a few cases where recursion is the best solution, but these assignments do not include any of those cases.
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Marylin said.
 
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marilyn�s comment is as good as any I�ve seen to explain the two techniques.
My background is procedural. I use recursion instinctively, but thanks to JavaRanch I'm beginning to break that habit. As Marilyn said, it has a "wow" or "cool" factor when you figure out an algorithm to reduce your code dramatically. The explanation of what was can get complex. Simple changes in the program might make the solution invalid, which might not be obvious to someone maintaining the code in the future.
She's also right that it is usually a lot harder to understand and troubleshoot recursion.
I tried to use recursion in several of my Java-1b, Java-4a, and Java-4b. By the time I passed those assignments my code had more lines, but it was self documenting and much simpler.
Bravo for a good style tip from JavaRanch!
 
Peter Gragert
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the whole functional programming world against a statement to replace recursiveness by iteration!
Why it is 'good': It is very often good voor thinking about a problem and how to solve it, it delivers an 'immediate' (a tiny bit exaggerated) solution.
Recursion is closely related to by proving something by induction and 'newbies (students)' need a lot of time to learn to do a proof by induction. For programming it may mean too that recusion is difficult for a 'beginner'.
But for 'us', see Marilyns opinion (which counts here)!
You see, my comment earlier in this thread comes true ;-).
Eventually one should give a worthwhile example. I will think about it ... ;-)
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Marilyn deQueiroz:
A recursive method is a method that calls itself either directly or indirectly.

Any problem that can be solved recursively can also be solved iteratively (nonrecursively).

... it is generally best to avoid. ...


Thank you for that wonderfully concise definition, Marilyn. This is what I'll latch on to, plus that bit of advice at the end.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!