0

What is the best way to implement the following pseudo-code in java?

method_one():
    while(condition_one):
        EXECUTE method_two();     // (A)

method_two():
    while(condition_two):
        EXECUTE method_three();   // (B)

method_three():
     if (condition_three):
        GOTO EXECUTE method_two();
     else:
        //do some stuff      

Edit: Thanks for prompt replies, everybody. Helped a lot. Figured it out now.

8
  • Do you literally want these to be three separate methods? Commented Feb 23, 2011 at 19:30
  • Will condition_one be true when condition_three is true in method_three? If so, you can just call method_one instead of a goto. I believe Java doesn't have tail call elimination, so you can't do that too often. Do you need three separate methods for the loops? It might be easier to write a single state machine in a single method that does the same logic. Commented Feb 23, 2011 at 19:31
  • okay, clarification time: method one and two do nothing but call the next one down the list, correct? Method 3 is the only one that does something interesting? Commented Feb 23, 2011 at 19:31
  • Your psuedo code is a fine example of "infinite recursion" If condition_three is true the loop never exits. Commented Feb 23, 2011 at 19:34
  • 1
    What is the difference between GOTO EXECUTE method_two(); and EXECUTE method_two() ? Commented Feb 23, 2011 at 19:34

3 Answers 3

4

If you don't need separate methods, this should be equivalent:

boolean exit_from_three = false;
while (condition_one || exit_from_three) {
    exit_from_three = false;
    while (condition_two) {
        if (condition_three) {exit_from_three = true; break;}
        // do some stuff
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Correct, the reason I did this way is to make it "look" simpler.
@blackened: I just fixed it (made it more complicated) to handle the case in which condition_three is true but condition_one isn't; your code does not exit the method_one loop in that case, so I edited mine to do the same.
3

I think you could do something like:

public void method_one() {
 while (condition_one) {
    method_two();
  }
}

public void method_two() {
  while (condition_two) {
    method_three();
  }
}

public void method_three() {
  if (condition_three) {
    return;
  } else {
    // do something magical
  }
}

3 Comments

Should the if in method_two be while?
How do you break out of that loop if condition_three is true? Note that the goto goes to the body of the loop in method_one.
Well, I guess I was assuming that there was some form of other checking going on here - so condition_two and condition_one aren't dependent on what happens in method_three. I mean, it could be updated by a separate thread or something. Or, it could just be a set of static variables that are set in the else clause of method_three. It could be dependent, or perhaps not.
0

Assuming I've read the pseudo-code right, it's a pretty simple case of calling methods. The only catch is that whatever represents conditions one, two, and three have to be updated somewhere or this will be an infinite loop at method_two (or method_one).

public class LoopingClass {

    private void method_one() {
        while(/* condition_one, where condition_one results in a boolean value */) {
            method_two();
        }
    }

    private void method_two() {
        while(/* condition_two, where condition_two results in a boolean value */) {
            method_three();
        }
    }

    private void method_three() {
        if(/* condition_three - where condition_three results in a boolean value */) {
            return;
        } else {
            // other stuff here
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.