SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: breaking loop!

  1. #1
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    breaking loop!

    OK, this method basically just alerts a user for input, and tests if a user has entered the string "conidtion1", "condition2" or "condition3".

    When I first send a message to this method containing either of the 3 conditions it accepts,and breaks the loop correctly as it should.

    If I enter anything other than the 3 desired strings, the loop continues as it should, HOWEVER, if i then enter one of my desired conditions (1 through 3) the loop does NOT break, and still continues. Any ideas why?

    many thanks guys!

    public String myMethod()
    {
    boolean myBool = true;
    String myString = MYDialog.request("Please enter condition1, condition2 or condition3");

    while(myBool = true)
    {
    if((myString.equals("condition1")) || (myString.equals("condition2")) || (myString.equals("condition3")))
    {
    myBool = false;
    return myString;
    }
    else
    {
    MYDialog.alert("Please enter a valid Operation!");
    myBool = true;
    this.getOperation();
    }
    }
    return null;
    }

  2. #2
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please format your code... like...
    Code:
    public String myMethod() {
        boolean myBool = true;
        String myString = MYDialog
                .request("Please enter condition1, condition2 or condition3");
    
        while (myBool = true) {
            if ((myString.equals("condition1"))
                    || (myString.equals("condition2"))
                    || (myString.equals("condition3"))) {
                myBool = false;
                return myString;
            } else {
                MYDialog.alert("Please enter a valid Operation!");
                myBool = true;
                this.getOperation();
            }
        }
        return null;
    }
    I'm not sure the method "getOperation" does but it won't change the value of myString since myString is local to the method myMethod.

    So this means when you call myMethod, myString will get set exactly one time (the second line).

    Also your loop will technically never break since your condition is (myBool = true). Remember "=" is assignment "==" is equality. So it should be (myBool == true), or as a shortcut Java lets you do just (myBool), or (!myBool) if you want to check for false. When I say "technically never break", what I'm saying is it seems like the loop breaks because of the return statement. Really the return statement is exiting the method altogether (which will break the loop).

    Hopefully this clears things up a bit.

  3. #3
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rozner View Post
    Please format your code... like...
    Code:
    public String myMethod() {
        boolean myBool = true;
        String myString = MYDialog
                .request("Please enter condition1, condition2 or condition3");
    
        while (myBool = true) {
            if ((myString.equals("condition1"))
                    || (myString.equals("condition2"))
                    || (myString.equals("condition3"))) {
                myBool = false;
                return myString;
            } else {
                MYDialog.alert("Please enter a valid Operation!");
                myBool = true;
                this.getOperation();
            }
        }
        return null;
    }
    I'm not sure the method "getOperation" does but it won't change the value of myString since myString is local to the method myMethod.
    Ahh, i copied that in by accident to my code, thats what was wrong. Thanks for the tips too, helps alot

  4. #4
    SitePoint Enthusiast badams67's Avatar
    Join Date
    Apr 2009
    Location
    London
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't forgot that you can break out of a loop at any time, using the BREAK
    keyword, and that jump to next loop iteration at any time using the CONTINUE
    keyword.
    Feed Distiller Aggregating RSS feeds into topical feeds.


Bookmarks

Posting Permissions

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