I have 2 fors, after the nested for I have some code which I don't want to execute if a condition is true inside the nested for. If I use break that code would execute, so (as I learned in SCJP) I used continue label;
for the outer for.
Is this a deprecated usage of Java ? Old fashioned ? Somebody suggested to use recursion or something else, but for me this is perfectly normal, simple, up-to-date and the perfect way of doing it.
here:
for (bla bla) {
for (bla bla) {
if (whatever) continue here;
}
// some code I don't want to execute if whatever is true
}
Thanks
Edited:
If I rephrase my question as: How can you 'navigate' between multiple nested fors ? This approach would be the 'recommended' way ? because this is what it says in SCJP Book. If not .. would this mean that Katherine Sierra
and Bert Bates
are wrong ?
Edited2:
Why is continue label;
discouraged ? I want an answer of the concepts or inside workings of OOP or Java, what might go wrong ..
The answer is: it depends. If you find yourself using continue
a lot then it might be a sign that your code needs a refactor. However, in the scenario you've given it seems like an OK place to use it.