Velocity Reviews > Java > Recursion-related problem

# Recursion-related problem

kelvSYC
Guest
Posts: n/a

 11-18-2003
I have a method that says something like this:

void foo(int b) {
// Do stuff and calculate the value of condition and anotherInt
if (condition) {
foo(anotherInt);
}
}

How can I change foo so that the inner foo is called only when the
outer foo is completed (ie. only one foo method is running)?

--
I am only a mirage.

Michael Borgwardt
Guest
Posts: n/a

 11-18-2003
kelvSYC wrote:
> void foo(int b) {
> // Do stuff and calculate the value of condition and anotherInt
> if (condition) {
> foo(anotherInt);
> }
> }
>
> How can I change foo so that the inner foo is called only when the
> outer foo is completed (ie. only one foo method is running)?

By elimination the recursion. How that can be done (and whether it can be done
at all) depends on the algorithm in question.

Basically, you need to replace the recursion with a loop, initialize the data
beforehand and then run the loop until the termination condition is met.

Eric Sosman
Guest
Posts: n/a

 11-18-2003
kelvSYC wrote:
>
> I have a method that says something like this:
>
> void foo(int b) {
> // Do stuff and calculate the value of condition and anotherInt
> if (condition) {
> foo(anotherInt);
> }
> }
>
> How can I change foo so that the inner foo is called only when the
> outer foo is completed (ie. only one foo method is running)?

void foo(int b) {
do {
// Do stuff and calculate the value of condition and anotherInt
b = anotherInt;
} while (! condition);
}

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules