• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How come there are 32 5s being printed ?

 
Ranch Hand
Posts: 68
1
  • Number of slices to send:
    Optional 'thank-you' note:
 
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
Please format your code correctly: single empty lines between successive methods.
Please use a pencil and paper to follow the order of execution. You will find the loop makes a recursive call repeatedly, and I shall leave it to you to count how many method calls that entails, and also work out why the value of a is when it is printed is 5.
 
Swapnil Mishra
Ranch Hand
Posts: 68
1
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:Please format your code correctly: single empty lines between successive methods.
Please use a pencil and paper to follow the order of execution. You will find the loop makes a recursive call repeatedly, and I shall leave it to you to count how many method calls that entails, and also work out why the value of a is when it is printed is 5.



ok

after step 4 step 2 and 3 are repeated back and forth until the condition turns false and breaks out of while loop and from here I am assuming after breaking out of the while loop it should print the last value of a i.e 5 and exit the program
image_2024-02-23_153551374.png
 
Swapnil Mishra
Ranch Hand
Posts: 68
1
  • Number of slices to send:
    Optional 'thank-you' note:
can someone please help
 
Bartender
Posts: 5469
212
  • Number of slices to send:
    Optional 'thank-you' note:
I put some println's in your code, so that you can see what's going on:
 
Swapnil Mishra
Ranch Hand
Posts: 68
1
  • Number of slices to send:
    Optional 'thank-you' note:
Thank you, this should help, I will take some time to understand this.
 
Swapnil Mishra
Ranch Hand
Posts: 68
1
  • Number of slices to send:
    Optional 'thank-you' note:
I am not able to understand, someone please explain with respect to the original code.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
Doing well It isn't difficult to work out how you get 5, is it?

When you call meth17(0), the loop runs 5×
When you call meth17(1), the loop runs 4×
When you call meth17(2), the loop runs thrice
When you call meth17(3), the loop runs twice
When you call meth17(4), the loop runs once

Each pass through the loop creates a recursive call. Those recursive calls call
meth17(++a). Take a larger sheet of paper, or write smaller, and draw a tree with all those calls on. I hope they will add up to 32 calls.
Note this is an inefficient way of running recursion, rather like the following notorious Fibonacci number program:-That program is expensive in terms of execution speed; it runs in O(1.618...) time complexity.
 
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
Here's a less verbose version:
Output:
(Note: '>>' is entering and '<<' is leaving)
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:
Or...
Output:
(Note: '++' is inside loop increment)

 
Marshal
Posts: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:
@Swapnil Mishra

Just want to say one thing, don't feel bad if you don't get it (now or even later). That's certainly not an easy thing to get!
Recursion on its own is mind bending, and then on top of that it is added a confusing code construct.

However, if I'd need to track myself what's going on (I certainly need ), I'd use this code:

Now, run this code, try to visualise in your head and code... I know, easier said than done, no worries if that takes a lot of time - expected.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

Liutauras Vilda wrote:. . . Recursion on its own is mind bending . . .

I gave up trying to understand it ages ago. It works, and that is that. (See Happiness by A A Milne.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
Did you get 32 occurrences of the System.out.println() call, Carey?
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

A few hours ago, I wrote:Did you get 32 occurrences . . .

I tried adding a count variable to your class and that did come to 32.
 
Ranch Hand
Posts: 49
1
  • Number of slices to send:
    Optional 'thank-you' note:
I'm going to take some time to study this myself.

On a different note, I think we've found a great way to generate some ski slopes, courtesy of Carey
 
Swapnil Mishra
Ranch Hand
Posts: 68
1
  • Number of slices to send:
    Optional 'thank-you' note:
After racking my brain for 1.5 days straight I finally understood the code on my own, and understood how recursions work inside the loop, feels soo satisfying when you kill a confusion. But thanks a ton, to each and everyone, it really really helped me a lot.

Sometimes staying in confusion really irritates you and pushes you to brings the best out of you, although I understand that using recursion inside the loop is not very practical, is something which you do not see very often, but ย sometimes my monkey mind poses such weird questions, which I like to solve and understand because It makes you understand how code really works and It also teaches you a lot and reveals many new information at microscopic level.

๐˜๐˜ฏ ๐˜ต๐˜ฉ๐˜ฆ ๐˜ค๐˜ข๐˜ท๐˜ฆ ๐˜ฐ๐˜ง ๐˜Š๐˜ฐ๐˜ฏ๐˜ง๐˜ถ๐˜ด๐˜ช๐˜ฐ๐˜ฏ, ๐˜Š๐˜ถ๐˜ณ๐˜ช๐˜ฐ๐˜ด๐˜ช๐˜ต๐˜บ ๐˜ช๐˜ด ๐˜ต๐˜ฉ๐˜ฆ ๐˜ฐ๐˜ฏ๐˜ญ๐˜บ ๐˜ณ๐˜ข๐˜บ ๐˜ฐ๐˜ง ๐˜ฉ๐˜ฐ๐˜ฑ๐˜ฆ.




Tried with smaller value of 'a' to reduce iterations and confusion (since 2 will be closer to base case condition)
also the initial call stack is colored to ease understanding

when a=2


meth17(3)โ†’meth17(4)โ†’meth17(5) prints 5  then unwind from stack meth17(5) prints 5

meth17(4)meth17(5) prints 5 then unwind from stack meth17(5) prints 5

meth17(3)meth17(4)meth17(5) prints 5 again unwinding from call stack meth17(5) prints 5 again meth17(4)meth17(5) prints 5 meth17(3)meth17(4)meth17(5) prints 5


This is my explanation, (skips condition checks)
It might not be as friendly to understand as compared to some of the explanations posted here, but please bear with me.


P.S : I recommend folks to avoid using pen and paper to dry run manually, and try the Eclipse Debugger instead with breakpoints at desired lines, while keeping the visual perspective setting on, it really helped a lot.
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:

Swapnil Mishra wrote:I understand that using recursion inside the loop is not very practical, is something which you do not see very often


Actually, recursion inside a loop happens quite often.
 
Swapnil Mishra
Ranch Hand
Posts: 68
1
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:

Swapnil Mishra wrote:I understand that using recursion inside the loop is not very practical, is something which you do not see very often


Actually, recursion inside a loop happens quite often.



Keeping the philosophical quotes aside, please don't tell me software developers in the industry actually come across these situations (recursion inside a loop).
because a day ago It was looking like a nightmare to me and impossible to comprehend. ๐Ÿ˜…๐Ÿ˜…๐Ÿ˜…
 
Chris Smithtopher
Ranch Hand
Posts: 49
1
  • Number of slices to send:
    Optional 'thank-you' note:

Swapnil Mishra wrote:

Carey Brown wrote:

Swapnil Mishra wrote:I understand that using recursion inside the loop is not very practical, is something which you do not see very often


Actually, recursion inside a loop happens quite often.



Keeping the philosophical quotes aside, please don't tell me software developers in the industry actually come across these situations (recursion inside a loop).
because a day ago It was looking like a nightmare to me and impossible to comprehend. ๐Ÿ˜…๐Ÿ˜…๐Ÿ˜…



Knowing life and people, I'm sure they encounter far worse, even if it's not intentional.
 
Swapnil Mishra
Ranch Hand
Posts: 68
1
  • Number of slices to send:
    Optional 'thank-you' note:

Chris Smithtopher wrote:Knowing life and people, I'm sure they encounter far worse, even if it's not intentional.



๐Ÿ˜Ÿ๐Ÿ˜ฏ
 
Carey Brown
Saloon Keeper
Posts: 10779
86
  • Number of slices to send:
    Optional 'thank-you' note:

Swapnil Mishra wrote:please don't tell me software developers in the industry actually come across these situations (recursion inside a loop).
because a day ago It was looking like a nightmare to me and impossible to comprehend. ๐Ÿ˜…๐Ÿ˜…๐Ÿ˜…


Traversing a "tree" happens quite frequently. Think about visiting all files in a directory tree.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

Yesterday, I wrote:. . . I gave up trying to understand [recursion] ages ago. . . .

 
Swapnil Mishra
Ranch Hand
Posts: 68
1
  • Number of slices to send:
    Optional 'thank-you' note:
when a=0

meth1meth2meth3meth4meth5 false prints 5
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth3meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth2meth3meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth3meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 flase prints 5
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth1meth2meth3meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth3meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth2meth3meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth3meth4meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth4meth5 false prints 5
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€meth5 false prints 5
 
Liutauras Vilda
Marshal
Posts: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:
?
 
Swapnil Mishra
Ranch Hand
Posts: 68
1
  • Number of slices to send:
    Optional 'thank-you' note:

Liutauras Vilda wrote:?



Hint : Try Tree Command in CMD and you might get the Hierarchical Structure of Directories (Directory Tree) You might get what I am trying to say

Also I'm quiet bad at explaining I used lots of shortcuts instead of writing prints 5 just wrote prints for meth17() wrote meth() sorry for that ๐Ÿ˜…
method 5 stacks upon method 4 , method 4 stacks upon method 3 upon method on 2 and finally on 1 and then unwinding starts from backwards (stacks executes in LIFO order)
 
Liutauras Vilda
Marshal
Posts: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:
Cowgratulations, Swapnil! Your topic has been published in our CodeRanch's February 2024 Edition Journal
 
Greenhorn
Posts: 3
  • Number of slices to send:
    Optional 'thank-you' note:
For investigation a nice try, but will ever in history a recursion inside
a while loop be needed as solution for a programming task?
Maybe in a rare case a loop inside a recursion.
I looked on internet in the search engine and see mostly
recursion OR a loop.
recursion for a recursive structure and a loop for a loop structure.
So i think be not worried about need of much study time.
It is maybe an insane mixture..?

 
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
No, recursion inside a loop is a normal thing.  See Carey's "Traversing a "tree" happens quite frequently" post above.
 
Swapnil Mishra
Ranch Hand
Posts: 68
1
  • Number of slices to send:
    Optional 'thank-you' note:

Liutauras Vilda wrote:Cowgratulations, Swapnil! Your topic has been published in our CodeRanch's February 2024 Edition Journal



๐Ÿ˜ฎ๐Ÿ˜€๐Ÿ˜ƒ Woa! Awesome!
 
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |