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
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
Code:
while (jpnl_mainMenu.bool_enabled)
{
System.out.println(bool_enabled);
}
Why does the loop only break if there is a print statement?
Re: While loop wont break unless there is a print statement in it
You need a label to break out of nested blocks:
Code:
outerLabel:
while (jpnl_mainMenu.bool_enabled)
{
if (!jpnl_mainMenu.bool_enabled)
{
break outerLabel;
}
}
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.
Code:
while (jpnl_mainMenu.bool_enabled)
{
if (!jpnl_mainMenu.bool_enabled)
{
System.out.println("g");
break;
}
}
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.