|
-
Aug 18th, 2003, 03:58 AM
#1
Thread Starter
Lively Member
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()?
-
Aug 18th, 2003, 05:04 AM
#2
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.
-
Aug 19th, 2003, 12:57 AM
#3
Dazed Member
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
}
}
Last edited by Dilenger4; Aug 19th, 2003 at 01:06 AM.
-
Aug 19th, 2003, 01:15 AM
#4
Dazed Member
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
}
}
}
Last edited by Dilenger4; Aug 19th, 2003 at 01:39 AM.
-
Aug 19th, 2003, 01:34 AM
#5
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.
-
Aug 19th, 2003, 01:37 AM
#6
Dazed Member
Problem is he dosen't want to reach a case. I think any case if i understand him correctly.
-
Aug 19th, 2003, 01:43 AM
#7
Dazed Member
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);
}
Last edited by Dilenger4; Aug 19th, 2003 at 01:54 AM.
-
Aug 20th, 2003, 01:08 AM
#8
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.
-
Aug 20th, 2003, 08:57 AM
#9
Thread Starter
Lively Member
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
-
Aug 21st, 2003, 01:29 AM
#10
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.
-
Aug 21st, 2003, 10:08 AM
#11
Dazed Member
Fantastic.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|