Recursion-related problem

Discussion in 'Java' started by kelvSYC, Nov 18, 2003.

  1. kelvSYC

    kelvSYC Guest

    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.
     
    kelvSYC, Nov 18, 2003
    #1
    1. Advertising

  2. 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.
     
    Michael Borgwardt, Nov 18, 2003
    #2
    1. Advertising

  3. kelvSYC

    Eric Sosman Guest

    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);
    }


    --
     
    Eric Sosman, Nov 18, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anakin
    Replies:
    14
    Views:
    748
    Thomas G. Marshall
    Apr 13, 2005
  2. JimC
    Replies:
    3
    Views:
    525
  3. Allan W
    Replies:
    4
    Views:
    529
    Jos A. Horsmeier
    Jan 22, 2004
  4. Maxwell Hammer
    Replies:
    7
    Views:
    647
    Peter Hansen
    Jun 18, 2005
  5. Replies:
    8
    Views:
    749
    John Reye
    Apr 26, 2012
Loading...

Share This Page