Win a copy of Rust Web Development this week in the Other Languages forum!
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# Stopping case a recursive and iterative code

Ranch Hand
Posts: 63
• Number of slices to send:
Optional 'thank-you' note:
Is the stopping case for the code that uses loop the variable n when it's 1? Also for the recursive method, is the stopping case when n is 1?

Code that uses loop to figure out the factorial of (n!)

Code that uses recursive to figure out the factorial of (n!)

Sheriff
Posts: 7113
184
• Number of slices to send:
Optional 'thank-you' note:
I believe 0! = 1, not 0. (That doesn't answer your questions, but I'm sure someone will.)

Knute Snortum
Sheriff
Posts: 7113
184
• Number of slices to send:
Optional 'thank-you' note:
Also, you recursive function is a little more complicated than it needs to be. Try getting rid of the factorial variable.

author
Posts: 23912
142
• Number of slices to send:
Optional 'thank-you' note:

It would also help if the OP would define what is meant by "stopping case".

For recursion, I would speculate that it is the "termination condition", which is the condition that is met, before recursion stops.

For iteration, that would be harder to speculate, as there are cases of n where the loop is never executed.

Henry

Aron Silvester
Ranch Hand
Posts: 63
• Number of slices to send:
Optional 'thank-you' note:

Knute Snortum wrote:I believe 0! = 1, not 0. (That doesn't answer your questions, but I'm sure someone will.)

Okay I fixed it. I returned 1 when n == 0 or n == 1.

Aron Silvester
Ranch Hand
Posts: 63
• Number of slices to send:
Optional 'thank-you' note:

Henry Wong wrote:
It would also help if the OP would define what is meant by "stopping case".

For recursion, I would speculate that it is the "termination condition", which is the condition that is met, before recursion stops.

For iteration, that would be harder to speculate, as there are cases of n where the loop is never executed.

Henry

Yes, you are right with your speculation. I actually think that the stopping case for both method is 0. What do you think about my proposed stopping cases?

Knute Snortum
Sheriff
Posts: 7113
184
• Number of slices to send:
Optional 'thank-you' note:
Well, I think n == 1 is the terminating condition in the recursive code, because you never get to n == 0 unless you enter 0.

Marshal
Posts: 8196
583
• Number of slices to send:
Optional 'thank-you' note:
By using ternary operator (? : ) you could get body of you factorial method as short as 1 line of code.

 got smile inside parenthesis, had to fix it.

Knute Snortum
Sheriff
Posts: 7113
184
• Number of slices to send:
Optional 'thank-you' note:
You got emoji spammed. (?:) You can disable smilies in a message.

Aron Silvester
Ranch Hand
Posts: 63
• Number of slices to send:
Optional 'thank-you' note:

Knute Snortum wrote:Well, I think n == 1 is the terminating condition in the recursive code, because you never get to n == 0 unless you enter 0.

You know what, now that you said it, it makes sense. Well a stopping case is basically what causes the methods to end. As for the loop method it stops when n is 0. When it's 0 it goes out of the while loop and returns the factorial. Correct?

Ranch Hand
Posts: 417
• Number of slices to send:
Optional 'thank-you' note:
Hello,

Just as an example; here is a recursive code example that build a flat list from an n-tree node (a parent can have an unlimited number of children). It would look pretty complex without recursion.

Here the stop condition occurs multiple times, it stops and pop the stack every time a node without a child is encountered and continue with the next child.

In prolog, you are required to use recursion since loops don't even exist! Code is usually only a few lines but require more thinking...

Marshal
Posts: 5217
323
• Number of slices to send:
Optional 'thank-you' note:
Conrado, while we don't mind if you cross post we do ask that you tell us that you have done so and cite where. Please read -> BeForthrightWhenCrossPostingToOtherSites

Exact same question asked at java-forums.org.

Aron Silvester
Ranch Hand
Posts: 63
• Number of slices to send:
Optional 'thank-you' note:

Tim Cooke wrote:Conrado, while we don't mind if you cross post we do ask that you tell us that you have done so and cite where. Please read -> BeForthrightWhenCrossPostingToOtherSites

Exact same question asked at java-forums.org.

Alright next time I will cite where I've cross post. Thanks for the heads up!

Aron Silvester
Ranch Hand
Posts: 63
• Number of slices to send:
Optional 'thank-you' note:

Knute Snortum wrote:Well, I think n == 1 is the terminating condition in the recursive code, because you never get to n == 0 unless you enter 0.

For the loop method it stops when n is 0. When it's 0 it goes out of the while loop and returns the factorial. Correct?

Knute Snortum
Sheriff
Posts: 7113
184
• 1
• Number of slices to send:
Optional 'thank-you' note:
That seems right to me.

Marshal
Posts: 74627
335
• Number of slices to send:
Optional 'thank-you' note:

Knute Snortum wrote:You got emoji spammed. (?:) . . .

You can also write &zwj; after the : and the emoji will vanish. That means zero‑width joiner.