0

I know i can skip loops in java from inner to outer this way:

loop1:
for (Object o : objects1){
   loop2:
   for (Object o : objects2){
       if (o.getSomething()) continue loop1 ;
   }
}

But how could I go the other way round, that is checking from outside if a loop is executed:

loop1:
for (Object o : objects1){
   if (condition) continue loop3;
   loop2:
   for (Object o : objects2){
       loop3:
       for (Object o : objects2){

       }
   }
}

if (condition) continue loop3; obviousely won't work, but you get the idea.

Why would I want to do this? I check every single combination of items with a lot of nested loops. Within the loops conditions are created. Based on these conditions not always all successing loops are necessary, thus skipping them would increase performance greatly.

Any ideas?

Trying to be more clear:

ItemSet<Item> set = new ItemSet<>();

for (Item a : itemtype1){
   for (Item b: itemtype2){
      set.add(a);
      set.add(b);
      if (set.getX() == 1) "execute from loop1 on"
      if (set.getX() == 2) "execute from loop2 on"

      loop1:
      for (Item c : itemtype3){
          loop2:
          for (Item d : itemtype4){
              //execute final code here - this must be reached in any case
          }
      }
   }
}
7
  • 3
    but you get the idea: no, I don't, unfortunately. Commented Feb 21, 2015 at 17:47
  • Maybe you should consider refactoring your code in order to use smaller methods instead. What you seek to do is typical of premature optimization, which is even more of a cardinal sin in Java that you have the JIT for that. Commented Feb 21, 2015 at 17:48
  • If I understand correctly, you want to skip the second loop, so it never iterates? I'm not sure there is a method of doing that or a good instance where this is a good idea. Do you have a more specific example? Commented Feb 21, 2015 at 17:50
  • You are declaring o in the inner and outter loop so your code wont compile. Your edit made things things even weirder, what you are trying is to jump to a certain line, which is not possible in java, and also what yould you expect to be the value of o1 from the outer loop if you skipped that line ? It's still in the scope but was never assigned anything Commented Feb 21, 2015 at 18:22
  • @reckter thats exactly what I want to do, with successing loops still executing Commented Feb 22, 2015 at 0:02

3 Answers 3

1

Make loop3 a method and call it when needed, ex:

void optimizedFunction(){
    loop1:
    for (Object o : objects1){
       if (condition) { loop3(objects2); continue;}
       loop2:
       for (Object o : objects2){
           loop3(objects2)
       }
    }
}



void loop3(List objects2){
       for (Object o : objects2){

       }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work when the method is not the most inner
0

Your question does not make much sense. Either a continue/break solves your problem or it can't. It's unclear what you really want to do.

Using a while-loop or an explicit for-loop instead will give you exact control about what is happening, for example if you have found an object in the inner loop that is the only one you are insterested in you can do something like this:

int iMin = 0;
int jMin = 0;
int iMax = object1.size();
int jMax = object2.size();

for(int i = iMin; i < iMax; i++) {
   for(int j = jMin; j < jMax; j++) {
      if(object1.get(i).someComparison(object2.get(j))) {
          jMin = i;
          jMax = i+1;
          break; //this will break the inner loop and continue the outer one
      }
   }
}

on the other hand you can always access a condition that was declared in an outer scope in an inner one so there is no reason why you would want to put a continue out of the loop you are trying to continue.

2 Comments

"Either a continue/break solves your problem or it can't." ...can be solved with conditions. "that is checking from outside if a loop is executed" your code checks from within. "on the other hand you can always access a condition that was declared in an outer scope" shure, but it takes lots of time to do so within 10 unneccessary loops when we are talkning about huge number.
like I said, tell me what you exactly want to do and I can help you, I don't even know what you mean. You are generalising down your question, but we need to know what exactly you want. delete the first part of the question about what you know, and instead provide more info about the problem.
-1

If your problem is that setting some condition inside inner loops won't allow your outer loops to see it, you can define the condition outside all loops to make it global for all loops.
If not please specify what are you want to achieve.

boolean condition;
loop1:
for (Object o : objects1){
   if (condition) continue loop3;
   loop2:
   for (Object o : objects2){
       loop3:
       for (Object o : objects2){
            if (something_trivial()){
                condition = false;
                continue loop1;
            }
       }
   }
}

3 Comments

This code doesn't compile and doesn't solve the problem. How will this code allow OP to "skip nested loops" ? It is not possible to jump into a nested loop as continue loop3 tries to do.
@MAV: OP's question is vague and not properly formulated. My example shows solution of one of the problems OP may wanted to solve.
The question "that is checking from outside if a loop is executed" couldn't be formulated more clearly. Your example shows exactly the opposite.

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.