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);
        }
    }
Ankur
  • 50,282
  • 110
  • 242
  • 312

1 Answers1

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.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758