This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Recursive Tracing

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I am struggling to understand recursive tracing in the follow scenario (code given below). I realize recursion has two cases - a base case, and a recursive case, which recalls the method. For example, given the following code:


Test(3) goes like this:

Test(3) evaluates to ? ---> test(2) evaluates to ? --- test(1) evaluates to 1, so now go back, up the chain.

We were given the following method as a class example, however I don't understand what two recursive calls, stacked on top on one another, behave like.



Upon running this code i know it prints "1, 2, 3, 4, 5" which almost makes sense to me but something is just not quite clicking. To be clear, this is for a class but is not a homework problem, just an example that was in the notes that I do not understand.

Thanks for any help!
 
Saloon Keeper
Posts: 6220
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might help you. Sprinkle in some print statements and include a "level" number to track the nesting of calls.
Output:
 
Marshal
Posts: 14025
233
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might also help to think about recursive cases as "going down the rabbit hole and (eventually) coming back".

That is, when you have two recursive calls "stacked up," as you say, like that the first call just goes as far down the recursion as it will, come back out when it reaches a base state, and completes the recursive call at that level. Then it continues to the second recursive call and does the same thing.

I think your confusion may lie in thinking about when the second recursive call occurs. That's where the different levels of recursion comes in. Carey's suggested adjustments to the code allows you to better see how recursion levels come into play. At the same level of recursion, program execution simply goes the regular sequential order.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!