2

Can I use break on a nested for-loop to get back to outer while-loop and use continue from inside the for-loop to force the while-loop to keep going? I can not get the for-loop conditions into my while-loop conditions so the while-loop might stop if I cannot continue on a specifically meet situation.

while(...some conditions...){
    ...print stuff
    for(...some instances are arrays, condition loop array...){
        if(...index meets conditions...){
            ...print once, some arrays might meet condition on multiple index
            break; //to prevent multiple printings
        }
    continue; //i don't want to force another while iteration if(false)
    //or is this continue for(loop) anyway?
    }
continue; //is this not essentially a while(true) loop with no return?
}

The reason I can not get the for-loop conditions into the while conditions is because there are more if conditions between the two loops like if(array == null) and if-condition x == true getArray() needs to be called if array is not passed in. Most of the time condition y and z print from while-loop but sometimes condition x is met so I need the for-loop. It's after the printing of the for-loop if(index true)) I need the while-loop to go again that I'm stuck with? Sometime this might happen from while-loop conditions anyway but I can see that it wont always, further more if for-loop if(index false)) is meet I don't want to force the while loop as this could get costly in run time processing and could possibly result in an endless loop.

PS I am a junior programer, I'm not even sure it this is possible? or makes sense, sorry if its a stupid question

3
  • 1
    You can use labels to break or continue loops stackoverflow.com/questions/886955/… Commented Mar 24, 2015 at 11:29
  • I am an experienced programmer, use break and continue, but would keep it simple. Having boolean active = ...; while (active) { offers a bit more control. Commented Mar 24, 2015 at 11:34
  • @usb I agree but without any other information it's one of the cleanest way to jump out of a loop if a certain condition is true. Commented Mar 24, 2015 at 12:35

3 Answers 3

9

you can name your loops like this:

namedLoop: for(...) {
    // access your namedloop
    break namedLoop;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Functions should replace all label-goto cases. Spaghetti code is a maintenance nightmare.
I am not saying that this is the best way to go and it surely hurts the readability and maintenance, but depending on the complexity of the designed code it is just a bit more simple (breaking a finished sorted array in a sorting routine for example)
@ Kevin can i do this as continue namedLoop; from inside the nested for(loop)?
2

You can break with label.

Here is a complete example showing it:

https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/java/nutsandbolts/examples/BreakWithLabelDemo.java

Basically the code is similar to this:

:myLabel


for (...) {
    for(...) {
        ...
        break myLabel; // Exit from both for loops
    }
}

Comments

0

continue and break apply to the immediate current scope, so if you're inside the for, it will apply to the for.

You can store the comparison result on a boolean variable to check if you want to continue.

I'm not a big fan of break and continue, it hinders readability in my opinion. You can acheive the same behavior using a different code structuring.

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.