Results 1 to 11 of 11

Thread: how to get out of recursive methods?

Hybrid View

  1. #1
    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
      }
     }
    }

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