How do I pause, delay,or stop, a loop from looping?

edited December 2013 in How To...

I'm using nested loops to search for a match inside a function, (eg. getMatch), but when I finally find my match, I want the function to stop looping. I've found the noLoop() but it seems to be only for draw()? What other way can I stop a for loop from continuing?

Tried exit() too, but I don't want to quit the program. I basically want to pause the loop so I can click on things inside the screen once the match has been made.

Thanks!

Answers

  • Hmm yes, this seems to be right. but I don't think it's working quite right for me. I can use this inside many layers inside of a nested loop, right?

    condensed code of interest:

                                                   //****************************** 4th LOOP SEARCH
                                                    try {
    
                                                    htmlList = new HtmlList(findLink3); 
                                                    ArrayList links4= (ArrayList) htmlList.getLinks(); 
    
                                                   for(int l = 8;l<38;l++){ 
    
                                                      String temp4 = links4.get(k).toString();
                                                      String [] wikiLink4Temp = split(temp4, "url:"); 
                                                      //println(wikiLink2[1]);
                                                      String []wikiLink4 = trim(wikiLink4Temp);
    
                                                      String findLink4 = baseURL + wikiLink4[1];
                                                      println( "Fourth search, item "  + k +" " +  findLink4);
    
                                                    for(int z=0; z < finalNodes.size(); z++){
                                           if (finalNodes.contains(findLink4) == true) {
    
                                                     println("We found link 4 " + findLink4 + " in finalNodes: " + finalNodes.get(z)+  " that is numberArray " + z);
                                                     println("This is link 1..." + findLink + " ..2.. " + findLink2 + " ..3..." + findLink3 + "..4... " + findLink4); 
    
                                                      String final_link = finalNodes.get(z).toString();     
                                                      background(250);
                                                    fill(100,100,100);
                                                    stroke(3);
                                                    line(nX,nY*2, nX, nY*5); 
                                                    println("should be displaying, why not displaying?");
                                                    noStroke();
                                                    node1.display(); // query
                                                    node2.display(); // findlink
                                                    node3.display(); // findlink2
                                                    node4.display(); // findlike3
                                                    node5.display(); // find link 4
                                                    node6.display(); // finalNode.get(z)
    
    
                                                            if (mousePressed){ 
                                                                            if (mouseX > nW && mouseX < nX+nW && mouseY > nH && mouseY < nY+nH) {
                                                                              link(URL_query);
                                                                              println("Lisa is the best!");
                                                                            } 
                                                                             if (mouseX > nW && mouseX < nX+nW && mouseY > nH*2 && mouseY < nY*2+nH) {
                                                                                link(findLink);
                                                                              println("Lisa is the 2nd best!");
                                                                            } 
                                                                            if (mouseX > nW && mouseX < nX+nW && mouseY > nH*3 && mouseY < nY*3+nH) {
                                                                              link(findLink2);
    
                                                                            } 
                                                                            if (mouseX > nW && mouseX < nX+nW && mouseY > nH*4 && mouseY < nY*4+nH) {
                                                                             link(findLink3);
    
                                                                            } 
                                                                            if (mouseX > nW && mouseX < nX+nW && mouseY > nH*5 && mouseY < nY*5+nH) {
                                                                             link(findLink4);
                                                                            }  
                                                                            if (mouseX > nW && mouseX < nX+nW && mouseY > nH*6 && mouseY < nY*6+nH) {
                                                                             link(final_link);
                                                                            }  
                                                                          }
                                                     break;
                                                   //foundConnection(); 
                                                  break;
    
    
                                                   }break;
                                          }
    
  • edited December 2013 Answer ✓

    You could use a boolean var and while loop combination.

    Here is pseudocode.

        boolean found = false
        while (!found) {
        ...
        // when you do find it
        found = true
        }
    

    Or perhaps use the boolean var in the test clause of your for loop.

    boolean found = false
    for (int i = 0; !found && i < 10 ; i++) {
            ...
            // when you do find it
      found = true;
    }
    
  • Thanks - very good idea!

  • "I can use this inside many layers inside of a nested loop, right?"
    I suppose you meant "can't".

    Little known fact (and rarely used): you can break from an inner loop to the outer one (whatever the level of nesting).

    outer:
    for (int i = 0; i < 10; i++)
    {
      for (int j = 0; j < 10; j++)
      {
        println("i: " + i + ", j: " + j + " -> " + (i + j));
        if (i + j == 15)
          break outer;
      }
    }
    exit();
    

    Close of goto (reserved work in Java, not implemented) and only usage of label (the line with :).

Sign In or Register to comment.