how can we go out from 4 inner for loops?
-
26-09-2019 - |
Question
Hi
I am beginner in java and my program has 4 for loops:
my program works like this that if b
is true
,the element will be removed from pointList and n
will be n--
and the I want to go out from all for loops and come again from the first for loop so l
will be l++
,how can i do this ? with break statement?
for (int l = 0; l < n; l++) {
for (int i = 1; i < (n - 2); i++) {
for (int j = i + 1; j < (n - 1); j++) {
for (int k = j + 1; k < n; k++) {
if (l != i && l != j && l != k) {
boolean b = isOK(pointList.get(l), pointList.get(i), pointList.get(j), pointList.get(k));
System.out.println(b);
if (b == true) {
pointList.remove(pointList.get(l);
n--;
break;
}
else
System.out.println(b);
}
}
}
}
}
Solution
You can make use of a labeled break as:
for (int l = 0; l < n; l++) {
foo: for (int i = 1; i < (n - 2); i++) {
for (int j = i + 1; j < (n - 1); j++) {
for (int k = j + 1; k < n; k++) {
if (l != i && l != j && l != k) {
boolean b = isOK(pointList.get(l), pointList.get(i), pointList.get(j), pointList.get(k));
System.out.println(b);
if (b == true) {
pointList.remove(pointList.get(l);
n--;
break foo;
}
else
System.out.println(b);
}
}
}
}
}
OTHER TIPS
In a loop the break
statement terminates the inner loop while continue
skips to the next iteration. In order for these two statements to work on a different loop to the inner one you need to use labels. Something like this should work:
outerloop:
for (int l = 0; l < n; l++) {
for (int i = 1; i < (n - 2); i++) {
for (int j = i + 1; j < (n - 1); j++) {
for (int k = j + 1; k < n; k++) {
if (l != i && l != j && l != k) {
boolean b = isOK(pointList.get(l), pointList.get(i), pointList.get(j), pointList.get(k));
System.out.println(b);
if (b == true) {
pointList.remove(pointList.get(l);
n--;
continue outerloop;
}
else
System.out.println(b);
}
}
}
}
}
Have a look at the labeled break statement
for example here: Branching Statements
String valueFromObj2 = null;
String valueFromObj4 = null;
OUTERMOST: for(Object1 object1: objects){
for(Object2 object2: object1){
//I get some value from object2
valueFromObj2 = object2.getSomeValue();
for(Object3 object3 : object2){
for(Object4 object4: object3){
//Finally I get some value from Object4.
valueFromObj4 = object4.getSomeValue();
//Compare with valueFromObj2 to decide either to break all the foreach loop
if( compareTwoVariable(valueFromObj2, valueFromObj4 )) {
break OUTERMOST;
}
}//fourth loop ends here
}//third loop ends here
}//second loop ends here
}//first loop ends here
Use a labeled loop
for (int l = 0; l < n; l++) {
loopa:
for (int i = 1; i < (n - 2); i++) {
for (int j = i + 1; j < (n - 1); j++) {
for (int k = j + 1; k < n; k++) {
if (l != i && l != j && l != k) {
boolean b = isOK(pointList.get(l), pointList.get(i), pointList.get(j), pointList.get(k));
System.out.println(b);
if (b == true) {
pointList.remove(pointList.get(l);
n--;
break loopa;
}
else
System.out.println(b);
}
}
}
}
}
and then break from the labeled loop
again:
for (int l = 0; l < n; l++) {
for (int i = 1; i < (n - 2); i++) {
for (int j = i + 1; j < (n - 1); j++) {
for (int k = j + 1; k < n; k++) {
if (l != i && l != j && l != k) {
boolean b = isOK(pointList.get(l), pointList.get(i), pointList.get(j), pointList.get(k));
System.out.println(b);
if (b == true) {
pointList.remove(pointList.get(l);
n--;
break again;
}
else
System.out.println(b);
}
}
}
}
}
I agree with all the other answers. However, I'd like to point out that an alternative to exit
would be to just put that code in its own routine and use a return
statement to break out of the whole thing. Your quad-nested loop is so complex by itself that it probably deserves to be in its own routine anyway.
I've worked on DoD jobs that required a cyclomatic complexity of no more than 6 for any one routine (with some exceptions). This series of loops alone is 4. If you can't find a simpler way to do it, you really ought to chuck them in their own routine just to preserve the sanity of the poor schmucks who have to maintain this code.
A first 'quick and dirty' solution would be to use a stay_into_loops
variable and modify the for
loops like:
boolean stay_into_loops = true
// here goes the first for loop
for (int i = 1; i < (n - 2) && stay_into_loops ; i++) {
for (int j = i + 1; j < (n - 1) && stay_into_loops ; j++) {
for (int k = j + 1; k < n && stay_into_loops ; k++) {
if (l != i && l != j && l != k) {
boolean b = isOK(pointList.get(l), `pointList.get(i), pointList.get(j), pointList.get(k));`
System.out.println(b);
if (b == true) {
pointList.remove(pointList.get(l);
n--;
stay_into_loops = false;
break;
However it is generally a code smell when you encounter things like these. Consider refactoring the code because this will escalate into a mess at some point.
Create an exit for yourself in each inner for loop.
Here's a quick and painless solution.
bool breakout;
for (int l = 0; l < n; l++)
{
breakout = false;
for (int i = 1; i < (n - 2) && !breakout; i++)
for (int j = i + 1; j < (n - 1) && !breakout; j++)
for (int k = j + 1; k < n && !breakout; k++)
{
if(b == true)
breakout = true;
}
}
So you see the breakout
boolean is your ticket out of each inner loop, coz it gets checked in every for
declaration.
And it gets reset every time the first for
iterates.