0

Is there a way to return some value from within a for loop without jumping out of the loop?

I am implementing a static analysis tool where I have to analyze a list of methods (CFGs) in a for loop. The size of CFG list is not known in advance. Each method in the for loop has to return some value. As asked above, is there a way to do it in a loop without breaking the loop? One possible alternative comes in mind is that I can unroll the loop, assuming the maximum list size could be some fixed value. But this does not solve the problem completely. Any help would be appreciated.

code looks like below.

for(CFG cfg: cfgList)
{
   val = analyze(cfg);
   return val;   //I want for loop not to stop here.
}

P.S. I cannot store the values in a list to return values later.

Edit1:

For example, consider following statements.

call method1();
st2;
st3;
...

This method1() can be any of five different methods. For all five possible options, I want to analyze each of them, return their values and analyze rest of the statements accordingly. So, I would analyze these 5 methods as below.

call method1-option1();
st2;
st3;
...

call method1-option2();
st2;
st3;
...

call method1-option3();
st2;
st3;
...

Hope, it helps in understanding the question.

8
  • 5
    "I cannot store the values in a list to return values later." Why not? Commented Mar 29, 2015 at 4:55
  • "I cannot store the values in a list to return values later" - that you can't looks to me the sensible solution. Or you can call a method from within loop. Can you explain briefly why you can't? Commented Mar 29, 2015 at 4:57
  • 2
    In other languages, you could use a generator/coroutine. In Java, you can't. This is usually (almost always) done by returning a List, or by passing in a listener (so that return val becomes listener.accept(val)). But since you've thrown in the odd requirement that you can't store the values in a list, it sounds like there's something non-standard in your requirements, so it's going to be hard for us to find you a good answer unless you explain those requirements a bit better. Commented Mar 29, 2015 at 5:02
  • 1
    @ajb python, for one... or is this some "python doesn't have real generators" comment? Commented Mar 29, 2015 at 5:08
  • 1
    @ajb Ah, got it! Yup, python has generators using the yield statement. Commented Mar 29, 2015 at 5:12

3 Answers 3

1

No you can not return value from loop without jumping out of it. According to your need you have to save value in other list and you can return that list after finishing the loop.

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

Comments

1

In Java 8, you can do:

Iterator<AnalysisResult> lazyAnalysisResults = cfgList.stream()
    .map(cfg -> analyze(cfg))
    .iterator();

And then the Iterator will supply new analyzed results one at a time, without you needing to collect them all into a list first.

Prior to Java 8, if you want your transformation to be lazy, the best you can do is to implement an Iterator yourself:

public final class AnalyzingIterator extends Iterator<AnalysisResult> {
  private final Iterator<CFG> iter;

  public AnalyzingIterator(Iterator<CFG> iter) {
    this.iter = iter;
  }

  @Override public boolean hasNext() {
    return iter.hasNext();
  }

  @Override public AnalysisResult next() {
    return analyze(iter.next());
  }

  @Override public boolean remove() {
    throw new UnsupportedOperationException();
  }
}

Comments

0

If you don't want to store results in a List and return it all together you can use callback mechanism.

Use analyze() to start a new thread passing cfg as well as reference to this. When processing is over make that processing thread call a callback method on your current instance / thread passing the analyzed value. Continue to do whatever you intend to do with this returned value in the callback method. And you don't have to alter your for loop.

3 Comments

Yes but that means you are blocking the main thread. That was just a suggestion for non blocking operation :)
Well, you're using the thread to do work, which is usually not the definition of blocking. Adding multithreading increases complexity, and you can actually see performance drop if there are few enough actions that the overhead of threading (context switching, syncing memory across cores, etc) is higher than the benefits of parallelism.
Yes I was taking about current operation and not block as in blocking CPU cycles. The problem you are stating might be a considerable one with handheld devices but should be ok with machines with sufficient RAM, SWAP space and cores.

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.