How to break from a loop after the condition is met

孤者浪人 提交于 2019-12-01 06:28:07

问题


Hi I have been trying for the past hour to break from this loop and continue since already met my condition once. My application pretty much reads a serie of lines and analyzes it and then prints the variable stated. An example of how the lines look like (the . are not included):

  • 10 c = 9+3
  • 20 a = c+1
  • 30 print c
  • 40 goto 20
  • 50 end

It does everything right, when it gets to line 40 goes to line 20 as expected, but i want it to go to line 50 since already went to line 40 once. Here is my code for this part:

while(booleanValue)
{
    if(aString.substring(0, 4).equals("goto"))
    {
        int chosenLine = Integer.parseInt(b.substring(5));
        if(inTheVector.contains(chosenLine))
        {
            analizeCommands(inTheVector.indexOf(chosenLine));
            i++;
        }
        else
        {
            System.ou.println("Line Was Not Found");
            i++;
        }
    }

    else if(aString.substring(0, 3).equals("end"))
        {
            System.out.println("Application Ended");
            booleanValue = false;
        }
}

回答1:


Use the break statement to break out of a loop completely once your condition has been met. Pol0nium's suggestion to use continue would not be correct since that stops the current iteration of the loop only.

while(foo)
{
    if(baz)
    {
        // Do something
    }
    else
    {
        // exit condition met
        break;
    }
}

All this having been said, good form dictates that you want clean entry and exit points so that an observer (maybe yourself, revisiting the code at a later date) can easily follow its flow. Consider altering the boolean that controls the while loop itself.

while(foo)
{
    if(baz)
    {
        // Do something
    }
    else
    {
        // Do something else
        foo = false;
    }
}

If, for some reason, you can't touch the boolean that controls the while loop, you need only compound the condition with a flag specifically to control your while:

while(foo && bar)
{
    if(baz)
    {
        // Do something
    }
    else
    {
        // Do something else
        bar = false;
    }
}



回答2:


You could keep the code using booleanValue as-is and switch to a do-while loop.

do {
 // ... existing code
} while (booleanValue);

However, to answer your specific question - you can always use the java break keyword. The continue keyword is more for skipping the remainder of the loop block and entering another loop iteration.

if you put this before your check for anything else, you would exit the loop immediately.

if(aString.substring(0, 3).equals("end")) {
    break;
}

As an additional tip, you may want to use String.contains("end") or String.endsWith("end") instead of substring(). This way if a 1 or 3 digit (or more) number is used your code will still work.



来源:https://stackoverflow.com/questions/16269589/how-to-break-from-a-loop-after-the-condition-is-met

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!