Results 1 to 11 of 11

Thread: how to get out of recursive methods?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103

    how to get out of recursive methods?

    okay:

    public int foo() {

    bar();

    return something;

    }

    public void bar(){

    bar();

    ...some base case to end the recursive method...

    }

    is there any way to end bar() (and all recursive bar()s that were called) without reaching the base case, and get back to foo()?

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Use return values. Let bar return a boolean and only execute base if bar returns true.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Unreachable statement.
    Code:
    public class X{
       public static void main(String[] args){
         foo();
     }
    
     public static void foo() {
        bar();
     }
    
     public static void bar(){
    
     return;
    
     bar(); // unreachable statement
       
     }
    }

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I can't see anyway that you could stop recursive call to bar() and get back to foo without using return values and reaching a case. If you exit the JVM before the call to bar then you will never get back to foo so that's not an option. Obviously you can't place return as the first statement in the method bar() since any statements that come after return will be unreachable. You would need to use return values.
    Code:
    public class C{
       public static void main(String[] args){
    
      foo();
     }
    
     public static void foo() {
        bar(true);
     }
    
     public static void bar(boolean b){
    
      if(b){
       System.out.println("1");   
      }else{
        bar(true); // never invoked
      }
     }
    }

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Let me adapt it to what I said.
    Code:
    public class C{
       public static void main(String[] args){
    
      foo();
     }
    
     public static void foo() {
        bar(true);
     }
    
     public static void bar(boolean b){
      if(b)
        System.out.println("1");
      else
        return bar(b);
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    If false was passed to bar() to make recursive calls wouldn't bar() have to have a boolean return type?

    Code:
    Posted by CornedBee
    
    public class C{
       public static void main(String[] args){
    
      foo();
     }
    
     public static void foo() {
        bar(true); // pass false instead
     }
    
     public static void bar(boolean b){
      if(b)
        System.out.println("1");
      else
        return bar(b);
    }

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Err, yeah, sorry, forgot to change that.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103
    thanks for the feedback ya'll. i tried using the boolean and it works. i sorta get the method to return "false" if something goes wrong, and when this returns to the method that called it, it will in turn check the result. so, a cascading effect of false returns will make its way back up because only when a method returns a true can things continue. or somethin like that

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Perfect
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width