Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2011
    Rep Power

    Default Looping a loop of loops

    Okay, I have a feeling there is a fairly simple way to do this, but I can't think of it with what I know.

    I have four methods (lets call them methodA, methodB, methodC, methodD).

    What I need to do is loop through methodA infinitely until I meet a certain condition. When it meets that condition I need to run methodB. Now methodB will run once and either kick me back into the infinite loop of methodA or run onto methodC. methodC will then run once and either kick back to methodA or go to methodD. Finally methodD either kicks back to methodA again or finally ends the loop. methodA also has a possibility to kick out of the loop, but that part I can handle because that is the objective which means my program can run its ending method.

    I know I could just call each method directly, but I also know from experience that if it loops too many times it will eventually crash due to taking up too much memory because the methods will never end.

    If it's important none of the methods have any required output.

    My current thought is that I can do a while loop, something like this:
    boolean needToLoop = true;

    with methodA changing needToLoop to false when it is ready to move on to methodB, but I'm not sure how to include the other methods properly.

    I feel like the solution here should be simple, but I'm just not quite seeing it. If I put the other methods inside the while loop then they'll run more often than I want them, but if I put them outside the while loop, I can't revisit the loop.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Rep Power

    Default Re: Looping a loop of loops

    Have methodB, C and D return some value (maybe a boolean) to methodA. Use that returned value to determine what to do next.

  3. #3
    Join Date
    Sep 2011
    Rep Power

    Default Re: Looping a loop of loops

    Okay, so loop like I have it for methodA to run it as often as I need, but instead of returning when I need it to advance, I just call methodB, have it return true/false, which will either end it to cause the loop to start over or call methodC. Okay, yeah that works. I knew it was something simple. Was just looking at it from too far out.

    Thanks for the help.

Similar Threads

  1. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  2. While loop not looping need help
    By MichaelT in forum New To Java
    Replies: 5
    Last Post: 03-06-2011, 09:26 PM
  3. Looping
    By Dean29126 in forum New To Java
    Replies: 3
    Last Post: 09-08-2010, 02:01 PM
  4. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 10:54 PM
  5. Looping Help Please
    By JonnySnip3r in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 05:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts