Results 1 to 4 of 4
  1. #1
    phillipie99 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default While loop wont break unless there is a print statement in it

    I am using a while loop to wait for actions in a jpanel to finish (occurs on button press in another class) but the while loop never breaks it self

    For example this will never break even when bool_enabled is set to false
    Java Code:
    while (jpnl_mainMenu.bool_enabled)
        	{
                  //even after adding this check it doesn't break, it is like the loop is not executing
                  if (!jpnl_mainMenu.bool_enabled)
                  {
                       break;
                  }
        	}
    This however, will break when bool_enabled is set to false
    Java Code:
    while (jpnl_mainMenu.bool_enabled)
        	{
                  System.out.println(bool_enabled);
        	}
    Why does the loop only break if there is a print statement?
    Last edited by phillipie99; 11-24-2012 at 09:53 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: While loop wont break unless there is a print statement in it

    You need a label to break out of nested blocks:

    Java Code:
            outerLabel:
            while (jpnl_mainMenu.bool_enabled)
            {
                  if (!jpnl_mainMenu.bool_enabled)
                  {
                       break outerLabel;
                  }
            }

  3. #3
    phillipie99 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: While loop wont break unless there is a print statement in it

    I don't think that's the problem as something like this wont get to the print statement (even though bool_enabled is false) and then would never reach the break but the code below the while is not executed so it hasn't broken out of the loop.
    Java Code:
    while (jpnl_mainMenu.bool_enabled)
        	{
        		if (!jpnl_mainMenu.bool_enabled)
        		{
        	             System.out.println("g");
                         break;
        		}
        	}

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: While loop wont break unless there is a print statement in it

    How is bool_enabled getting set to false?
    There's nothing in that sample that shows it being false.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  2. break statement is ignored.
    By Pojahn_M in forum New To Java
    Replies: 11
    Last Post: 05-09-2011, 09:35 PM
  3. Replies: 10
    Last Post: 01-07-2011, 10:28 AM
  4. Can you print to a file using page break?
    By kahoots in forum New To Java
    Replies: 14
    Last Post: 07-02-2010, 02:06 PM
  5. break statement
    By AbdulAziz Bader in forum New To Java
    Replies: 17
    Last Post: 06-02-2010, 03:42 PM

Posting Permissions

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