I\'ve got a nested loop construct like this:
for (Type type : types) {
for (Type t : types2) {
if (some condition) {
// Do somethin
for (int j = 0; j < 5; j++) //inner loop
should be replaced with
for (int j = 0; j < 5 && !exitloops; j++)
.
Here, in this case complete nested loops should be exit if condition is True
. But if we use exitloops
only to the upper loop
for (int i = 0; i < 5 && !exitloops; i++) //upper loop
Then inner loop will continues, because there is no extra flag that notify this inner loop to exit.
Example : if
i = 3
andj=2
then condition isfalse
. But in next iteration of inner loopj=3
then condition(i*j)
become9
which istrue
but inner loop will be continue tillj
become5
.
So, it must use exitloops
to the inner loops too.
boolean exitloops = false;
for (int i = 0; i < 5 && !exitloops; i++) { //here should exitloops as a Conditional Statement to get out from the loops if exitloops become true.
for (int j = 0; j < 5 && !exitloops; j++) { //here should also use exitloops as a Conditional Statement.
if (i * j > 6) {
exitloops = true;
System.out.println("Inner loop still Continues For i * j is => "+i*j);
break;
}
System.out.println(i*j);
}
}