Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Convert this to non-recursive

1. ## Convert this to non-recursive

```public static int f(int a, int b)
{
Z.h1(a, b);           //// arbitrary code

if (Z.h2(a, b))
return Z.h3(a, b);

else
{
Z.h4(a, b);        ////arbitrary code
return f(Z.h5(a, b), Z.h6(a, b));      //// recursive call
}
}```

-h1 and h4 --------------------> void functions
-h2 ------------------------------> boolean function
-h3, h5 and h6 -------------->return int
-h1 ... h6-----------------------> will not call f

My Attempt to convert the code above to non-recursive:

```
public static int f(int a, int b)
{
while(true)
{
Z.h1(a, b); //// arbitrary code

if (Z.h2(a, b)) //// base case
return Z.h3(a, b);
else
{
Z.h4(a, b); /////arbitrary code

int temp = Z.h5(a, b);
b = Z.h6(a, b));
a=temp;
}
}
}```

I'm not sure if I'm right.
Any ideas?

2. ## Re: Convert this to non-recursive

It looks fine to me. Did you test it out to make sure the outputs match up?