1

I want to break the outer while loop when the condition stop=true is reached in the inner for loop. Is this possible?

urlsToScrape.addAll(startUrls);

    boolean stop=false;

    while(stop==false){
        for(Link url:urlsToScrape){
p.rint(url.depth+" >= "+depth);
            if(url.depth>=depth){
                p.rint("STOP!");
                stop=true;
                break;
            }
p.rint("scrape(): "+url.link+" / "+url.depth);
            processLinks(url.link,url.depth);
            urlsToScrape.remove(url);
            scrapedUrls.add(url);
        }
    }
5
  • 1
    while(!stop){ is a better style than while(stop==false){ Commented Jun 11, 2013 at 13:37
  • Isn't that what already happens? The entire body of the while loop is the for loop, so if you set stop to true inside that, then break out of it, the while loop will stop iterating. Commented Jun 11, 2013 at 13:38
  • Sorry this is a duplicate. Feel free to vote to close. Commented Jun 11, 2013 at 13:38
  • @Anthony I thought that was how it should work, but it didn't seem to. The label technique worked. Commented Jun 11, 2013 at 13:39
  • jlordo +1 - you're right. Commented Jun 11, 2013 at 13:41

1 Answer 1

10

Use a label :

 outofthere:
 while (stop==false){
      for(Link url:urlsToScrape){
            p.rint(url.depth+" >= "+depth);
            if(url.depth>=depth){
                p.rint("STOP!");
                stop=true;
                break outofthere;
            }
            p.rint("scrape(): "+url.link+" / "+url.depth);
            processLinks(url.link,url.depth);
            urlsToScrape.remove(url);
            scrapedUrls.add(url);
      }
  }

See Oracle's documentation.

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

2 Comments

+1. I always forget to use label in loop and sometimes they are very useful.
@Ankur Just waiting for what exactly ? :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.