java - My return is not stopping execution -


i have return, can see line "this not print" shouldn't reached after return happens gets called.

what's going on?

code

here's entire procedure, it's rough copy @ moment...:

private void greedysearch (string lookfornode) {     // note: available vars     // reqstartnode     // reqendnode      // search through entire tree looking for...     system.out.println("searching through entire tree looking "+lookfornode);     (int = 0; < treelist.size(); i++) {          data currentnode = treelist.get(i);          // ... reqstartnode         if (currentnode.getnodename().equals(lookfornode))         {                system.out.println("found matching node. currentnode.getnodename=" + currentnode.getnodename()+" lookfornode="+lookfornode);              // check see if there's children?             if (currentnode.childrenlist.size() > 0)             {                 // find smallest child node                 double smallestheuristic = currentnode.childrenlist.get(0).getheuristic();                 string smallestnode = currentnode.childrenlist.get(0).getnodename();                 (int ii = 1; ii < currentnode.childrenlist.size(); ii++)                 {                     if (currentnode.childrenlist.get(ii).getheuristic() < smallestheuristic)                     {                         smallestheuristic = currentnode.childrenlist.get(ii).getheuristic();                         smallestnode = currentnode.childrenlist.get(ii).getnodename();                     }                 }                  // check see if smallest child node reqendnode                 if (smallestnode == reqendnode)                 {                     system.out.println("found goal "+smallestnode);                      // quit because found answer                     return;                 }                 // expand node                 else                 {                     greedysearch (smallestnode);                 }             }             // no children, we've reached end             else             {                 system.out.println("we've reached end @ "+currentnode.getnodename());                  // quit because we've reached no further children expand                 return;             }             system.out.println("this not print");               }         else         {             system.out.println("skipped node "+currentnode.getnodename());         }     }      system.out.println("finished search");  } 

edit:

the correct solution realised doing return after call recursive procedure so:

greedysearch (smallestnode); // quit because going recursive, our job here done return; 

my ouput now:

searching through entire tree looking s skipped node skipped node b skipped node c skipped node d skipped node e skipped node f skipped node g skipped node g found matching node. currentnode.getnodename=s lookfornode=s searching through entire tree looking found matching node. currentnode.getnodename=a lookfornode=a searching through entire tree looking b skipped node found matching node. currentnode.getnodename=b lookfornode=b searching through entire tree looking c skipped node skipped node b found matching node. currentnode.getnodename=c lookfornode=c we've reached end @ c 

nothing strange going on. can see @ least 1 code path can happen.

in nested call, executed:

        else         {             system.out.println("we've reached end @ "+currentnode.getnodename());              // quit because we've reached no further children expand             return;         } 

then returning outer call:

            else             {                 greedysearch (smallestnode); // resuming here...             }         }         else         {             // ...all skipped (because in else block             // of if true)...         }         // ...and printed.         system.out.println("this not print");           } 

in other words, while 2 lines you're looking @ are, indeed, mutually exclusive during 1 call of recursive method, not mutually exclusive between 2 nested calls. , messages print can appear in sequence, case in output.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -