• 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

Doubt: Recursion!

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Demo1 {
public static void main(String args[]) {
myMethod(5); // pass positive integer
}

static void myMethod( int counter)
{
if(counter == 0)
return;

else{
System.out.println("hello " + counter);
myMethod(--counter);

System.out.println(" "+counter);
return;
   }
}
}

produces result

hello 5
hello 4
hello 3
hello 2
hello 1
0
1
2
3
4


Please explain how 0 to 4 are printed?
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what you mean by "how" - by the second println statement, obviously. That happens after the recursion, so they're printed last.
 
Jk Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When counter equals zero the execution enters if block function returns. Then how 0 1 2 3 4 are printed?
 
Jk Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First myMethod() is called with argument 5 (counter=5). Then it calls itself four times with counter = 4,3,2,1 progressively, entering else block everytime. The six time when counter equals '0', the control enters IF block and returns from function. So, please tell me how 0,1,2,3,4 gets printed?
 
Marshal
Posts: 79183
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have to try it with a pencil and paper; write down the five method calls, and notice the order and location of the print instructions.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fix the indentation of myMetho, and add braces to make the flow clearer:

After the fifth entry, counter is zero and the method returns.
Now line 10 is executed, when counter equals zero, so "0" is printed.
Then the method returns to the fourth invocation where line 8 was executed with the value of 1 as the argument to myMethod, and line 10 is executed again, now with counter=1.
Then that invocation returns to the third invocation of myMethod, where line 8 was executed with an argument value of 2, so line 10 is again executed and prints "2".

this continues on until the first invocation of myMethod , where counter is now 4, so line 10 is executed and "4" is printed. This time the return returns to main() and the program ends.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this code snippet is a good example why counter - 1 is preferred option and not --counter, and that is mainly because latter variant has side-effects. So really 0 belongs not to that level of stack where it is actually printed.

Change line 9 to counter - 1 to see what I mean. I equally feel, that not many would agree with me.
 
Jk Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the ELSE block contains all following 4 statements, not just the first one, call to System.out.println()

                       Else{
                             System.out.println("hello " + counter);
     myMethod(--counter);
     System.out.println(" "+counter);
     return;
                       }
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the thing with recursion; myMethodd calls itself, thus running the entire body with a different parameter first - only then is the second println statement executed. And that happens at every level of the recursion. Thus all instances of the second println are executed after all instances of the first.
 
Jk Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have understood. Thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic