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

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Josh Waitere
  • 51
  • 1
  • 4
  • 1
    You can use labels to break or continue loops http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java – Flown Mar 24 '15 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. – Joop Eggen Mar 24 '15 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. – Flown Mar 24 '15 at 12:35

3 Answers3

9

you can name your loops like this:

namedLoop: for(...) {
    // access your namedloop
    break namedLoop;
}
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
  • 3
    Functions should replace all label-goto cases. Spaghetti code is a maintenance nightmare. – Mr. Polywhirl Mar 24 '15 at 11:37
  • 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) – SomeJavaGuy Mar 24 '15 at 11:44
  • @ Kevin can i do this as `continue namedLoop;` from inside the nested for(loop)? – Josh Waitere Mar 24 '15 at 12:16
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
    }
}
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
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.

m0skit0
  • 25,268
  • 11
  • 79
  • 127