Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

recursive function!!!!

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All ,

Here i posted simple recursive function..but i wonder about its output..
-----------------------------------------------------------------

public class Java extends Java1 {


void myMethod( int counter)
{
if(counter == 0){
System.err.println("couter is zero");
return;
}else
{
System.out.println("hello" + counter);
myMethod(--counter);
System.out.println("%%"+counter);
return;
}
}
public static void main(String[] args) {

Java ja=new Java();
ja.myMethod(5);

}

}

------------------------------------------------------

output:
------------------------------------------------------
hello5
hello4
hello3
hello2
hello1
%%0
%%1
%%2
%%3
%%4
couter is zero
-------------------------------------------

but what i expected is
--------------------------------------
hello5
hello4
hello3
hello2
hello1
couter is zero
----------------------------------

please anyone can explain me ..................


thanks & regards,
seetharaman
 
Amit M Tank
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its hard to explain. The line System.out.println("%%"+counter); also gets executed. You need to put the program in debugger and watch the execution in order to understand this.
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please anyone can explain me ..................


Basically, some of your code is writing to standard out. And some of you code is writing to standard err. These are two different streams -- and the order that they get written to screen is the order that they get flushed.

Change *all* your writes to write to the same stream, and you should get what you expected.

Henry
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

thanks for earnest reply!! i changed streams as per your advise..

now i get output like below
------------------------------
hello5
hello4
hello3
hello2
hello1
couter is zero
%%0
%%1
%%2
%%3
%%4
--------------------------------------

but after "counter is zero",it have to stop know?..

i debugged ..but i cannot understand..

please explain me...

thanks®ards,
seetharaman
 
Rodrigo Lopes
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call myMethod(5), it'll:

  • 1. print "hello5"
  • 2. call myMethod(4)
  • 3. print "%%4"


  • Then, the call to myMethod(4) will:
  • 2.1. print "hello4"
  • 2.2. call myMethod(3)
  • 2.3. print "%%3"


  • and so on
    [ April 21, 2008: Message edited by: R Lopes ]
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Java Windows XP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Lopes,

    thanks for your earnest reply.your explanation and my output is different..

    please can you explain me clearly

    thanks & regards,
    seetharaman
     
    Vikas Kapoor
    Ranch Hand
    Posts: 1374
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's pretty simple.

    Let's consider you are calling myMthod with the value 2.

    You are in the method,

    1)First it will test for the value of counter variable and it will go in else part.There it prints its value(counter=2).After that you call the same method with one less of counter value(myMethod(1)). Here, yet method hasn't been executed completely because of the same method call again(myMethod(1)).This statement System.out.println("%%"+counter); is still left to be executed.Don't worry soon it will get the chance to execute ,too.

    OUTPUT:
    hello2

    2)Now counter=1 , again it will go into if else part.Priting the value of counter and again the method call(myMethod(0)).Again the same statement will not get the chance to execute.

    OUTPUT:
    hello2
    hello1

    3)Now counter=0 , SO it will jump into if part.There it will print 'couter is zero'.And will return.

    OUTPUT:
    hello2
    hello1
    counter is zero.

    As something is still left (System.out.println("%%"+counter); Remember!) to be executed now it will get the chance to execute.

    So the output should be something like,

    hello2
    hello1
    counter is zero
    %%1 (Here,It's 1 because counter has been decremented to 1).
    %%0 (same here...)



    If you have some idea of stack then the same thing could have been explained more beautifully.
    [ April 21, 2008: Message edited by: Vishal Pandya ]
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Java Windows XP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks Vishal Pandya ..
     
    Rodrigo Lopes
    Ranch Hand
    Posts: 119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by seetharam venk:
    Hi Lopes,

    thanks for your earnest reply. your explanation and my output is different..

    please can you explain me clearly

    thanks & regards,
    seetharaman


    Sorry if it was not clear, but it is the same output...
    I'll not explain again what Vishal Pandya already did
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Java Windows XP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi friends,

    well as per vishal explanation,output is below

    -----------------------
    hello2
    hello1
    counter is zero
    %%1 (Here,It's 1 because counter has been decremented to 1).
    %%0 (same here...)
    -------------------------

    see here after counter is zero,then it will return to else block and it will continue ..so it execute %%0(not%%1)...then it is return..so output should be
    -------------
    hello2
    hello1
    counter is zero
    %%0 (Here,It's 0 because counter has been decremented to 0).
    ------------------
    but wen i run i get
    ----------------
    hello2
    hello1
    counter is zero
    %%0
    %%1
    ------------------- HOW?(please explain me last 2 lines of output!!!)

    TO BE HONEST STILL I DO NOT GET YOU.....(SORRY!!!)


    thanks & regards,
    seetharaman
     
    Vikas Kapoor
    Ranch Hand
    Posts: 1374
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi seetharam,

    Sorry it was a Typo.

    The correct output is:

    hello2
    hello1
    couter is zero
    %%0
    %%1

    I think this is what you get.

    To continue with our example the statement System.out.println("%%" + counter); is left to be executed twice.Here's the concept of stack comes into picture.

    Stack works on LAST IN FIRST OUT base. So first time it will insert System.out.println("%%" + counter); with the counter value 1 and then 0.
    As it is stack the last one will be popped first. So in output first you find %%0 and then %%1.

    How's this?
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Java Windows XP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi vishal,
    once again thanking you...

    ----------------------------------------------------------------
    To continue with our example the statement System.out.println("%%" + counter); is left to be executed twice.Here's the concept of stack comes into picture.
    ----------------------------------------------

    System.out.println("%%" + counter); is left to be executed twice.

    YES..correct it left to be executed twice...i understand the stack concept(LIFO)output series no problem...BUT have it automatically executed twice!!!you mean whether recursive method will do automatically twice,IF
    it left to be executed twice..!!...

    friend i am waitng for your earnest reply
     
    Rodrigo Lopes
    Ranch Hand
    Posts: 119
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Trying again

    When you call myMethod(5), it'll:


  • Steps 2.1, 2.2 and 2.3 are result of the call do myMethod(4) in step 2.
  • Steps 2.2.1, 2.2.2 and 2.2.3 are result of the call do myMethod(3) in step 2.2.
  • Steps 2.2.2.1, 2.2.2.2 and 2.2.2.3 are result of the call do myMethod(2) in step 2.2.2.
  • Steps 2.2.2.2.1, 2.2.2.2.2 and 2.2.2.2.3 are result of the call do myMethod(1) in step 2.2.2.2.
  • Step 2.2.2.2.2.1 is result of the call do myMethod(0) in step 2.2.2.2.2.

  •  
    jaspreet atwal
    Ranch Hand
    Posts: 52
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by R Lopes:
    Trying again

    When you call myMethod(5), it'll:


  • Steps 2.1, 2.2 and 2.3 are result of the call do myMethod(4) in step 2.
  • Steps 2.2.1, 2.2.2 and 2.2.3 are result of the call do myMethod(3) in step 2.2.
  • Steps 2.2.2.1, 2.2.2.2 and 2.2.2.3 are result of the call do myMethod(2) in step 2.2.2.
  • Steps 2.2.2.2.1, 2.2.2.2.2 and 2.2.2.2.3 are result of the call do myMethod(1) in step 2.2.2.2.
  • Step 2.2.2.2.2.1 is result of the call do myMethod(0) in step 2.2.2.2.2.



  • Yeah, that being said...
    In Order to get the following Output...

    but what i expected is
    --------------------------------------
    hello5
    hello4
    hello3
    hello2
    hello1
    couter is zero

    You need to change 'return' in your if statement below to something that will exit the program. Currently It is just ending the method call...

    if(counter == 0){
    System.err.println("couter is zero");
    return;
     
    Vikas Kapoor
    Ranch Hand
    Posts: 1374
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by seetharam venk:
    you mean whether recursive method will do automatically twice,IF
    it left to be executed twice..!!...


    yes, It is handled automatically. Whatever is left in the stack will be popped up automatically.

    Now, if it is clear then try to play with your programme. You will have some more idea. I forgot to mention that if you have any IDE(eclipse,Netbeans) then
    the debugger will give very clear idea. But at the inital stage it is not suggested to use IDEs.
     
    Gavin Tranter
    Ranch Hand
    Posts: 333
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It looks to me like you are getting the correct output, and your expectations are some what off.

    I wont explain what others have, however I will add that you get 3 "types" of recursion.
    HEAD, MID(?) and TAIL, depending on were your recursive call is.

    What you have is MID, in that you do some work, make the call, this leaves work to be done, which (as others have explained) is executed on teh return though the stack (LIFO).

    The if statement checking for the error condition is teh first thing that is executed and starts the return process, so this will always be before the %% printouts.

    Recursion is very powerful tool for some situation, but you should becareful as you can, if your stack is deep enough, overflow the stack.

    Gavin
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Java Windows XP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    oh....i got it...

    cheers vishal,lopes and jaspreet
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic