will javascript “break” stop a recursive function completely or just that instance?












0















If 'break' is called inside a recursive function will it break all the iterations of it being called or will it just cancel that instance? Specifically I want to know about the following code:



//take a 2d array and check if it is in _switch
function checkSwitch(arr2d, rates, n = 0, found = ){
if(n===arr2d.length){
inCase(found, rates)
break
}
for(let i in arr2d[n]){
if(deepCheck(_switch, [...found, arr2d[n][i]]))
checkSwitch(arr2d, rates, n+1, [...found, arr2d[n][i]])
}
return false
}


I have a 2d array (array of arrays) and I check if one element from each of the arrays is included as nested object keys in this _switch I am making. When I have an all match then run a function, and I want to continue checking the 2d array to see if there are other matches.



However, if I ever come across an array with no matches in it then I need to break all iterations of the recursion for efficiency because I know there cannot logically be any matches of one from each.



So I need to be able to break one instance and to break all of them. I am not sure how to obtain this.



CORRECTED CODE AND LESSONS LEARNED
I am amazed at how much I am learning about what intellignece actually is while working on artificial intelligence. In a recursive function adding in a return value will break the current instance and if you check the return when calling the recursion then you can passback the collapse of the entire recursion. Here is the new code:



function checkSwitch(arr2d, rates, n = 0, found = ) {
if (n === arr2d.length) {
inCase(found, rates);
return true;
}
let tf = false
for (let i in arr2d[n]) {
if (deepCheck(_switch, [...found, arr2d[n][i]])) {
if (!checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
return false;
} else {
tf = true
}
}
}
return tf;
}


Returning true means break this iteration and keep going, while returning false means break everything. I added in a toggle right before the for loop so that if we happen to get a match of one from every array, then logically we need to check every other possibility because they may be valid. otherwise if we get through the for without getting a true then break everything because there are no longer any possibilities.










share|improve this question





























    0















    If 'break' is called inside a recursive function will it break all the iterations of it being called or will it just cancel that instance? Specifically I want to know about the following code:



    //take a 2d array and check if it is in _switch
    function checkSwitch(arr2d, rates, n = 0, found = ){
    if(n===arr2d.length){
    inCase(found, rates)
    break
    }
    for(let i in arr2d[n]){
    if(deepCheck(_switch, [...found, arr2d[n][i]]))
    checkSwitch(arr2d, rates, n+1, [...found, arr2d[n][i]])
    }
    return false
    }


    I have a 2d array (array of arrays) and I check if one element from each of the arrays is included as nested object keys in this _switch I am making. When I have an all match then run a function, and I want to continue checking the 2d array to see if there are other matches.



    However, if I ever come across an array with no matches in it then I need to break all iterations of the recursion for efficiency because I know there cannot logically be any matches of one from each.



    So I need to be able to break one instance and to break all of them. I am not sure how to obtain this.



    CORRECTED CODE AND LESSONS LEARNED
    I am amazed at how much I am learning about what intellignece actually is while working on artificial intelligence. In a recursive function adding in a return value will break the current instance and if you check the return when calling the recursion then you can passback the collapse of the entire recursion. Here is the new code:



    function checkSwitch(arr2d, rates, n = 0, found = ) {
    if (n === arr2d.length) {
    inCase(found, rates);
    return true;
    }
    let tf = false
    for (let i in arr2d[n]) {
    if (deepCheck(_switch, [...found, arr2d[n][i]])) {
    if (!checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
    return false;
    } else {
    tf = true
    }
    }
    }
    return tf;
    }


    Returning true means break this iteration and keep going, while returning false means break everything. I added in a toggle right before the for loop so that if we happen to get a match of one from every array, then logically we need to check every other possibility because they may be valid. otherwise if we get through the for without getting a true then break everything because there are no longer any possibilities.










    share|improve this question



























      0












      0








      0








      If 'break' is called inside a recursive function will it break all the iterations of it being called or will it just cancel that instance? Specifically I want to know about the following code:



      //take a 2d array and check if it is in _switch
      function checkSwitch(arr2d, rates, n = 0, found = ){
      if(n===arr2d.length){
      inCase(found, rates)
      break
      }
      for(let i in arr2d[n]){
      if(deepCheck(_switch, [...found, arr2d[n][i]]))
      checkSwitch(arr2d, rates, n+1, [...found, arr2d[n][i]])
      }
      return false
      }


      I have a 2d array (array of arrays) and I check if one element from each of the arrays is included as nested object keys in this _switch I am making. When I have an all match then run a function, and I want to continue checking the 2d array to see if there are other matches.



      However, if I ever come across an array with no matches in it then I need to break all iterations of the recursion for efficiency because I know there cannot logically be any matches of one from each.



      So I need to be able to break one instance and to break all of them. I am not sure how to obtain this.



      CORRECTED CODE AND LESSONS LEARNED
      I am amazed at how much I am learning about what intellignece actually is while working on artificial intelligence. In a recursive function adding in a return value will break the current instance and if you check the return when calling the recursion then you can passback the collapse of the entire recursion. Here is the new code:



      function checkSwitch(arr2d, rates, n = 0, found = ) {
      if (n === arr2d.length) {
      inCase(found, rates);
      return true;
      }
      let tf = false
      for (let i in arr2d[n]) {
      if (deepCheck(_switch, [...found, arr2d[n][i]])) {
      if (!checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
      return false;
      } else {
      tf = true
      }
      }
      }
      return tf;
      }


      Returning true means break this iteration and keep going, while returning false means break everything. I added in a toggle right before the for loop so that if we happen to get a match of one from every array, then logically we need to check every other possibility because they may be valid. otherwise if we get through the for without getting a true then break everything because there are no longer any possibilities.










      share|improve this question
















      If 'break' is called inside a recursive function will it break all the iterations of it being called or will it just cancel that instance? Specifically I want to know about the following code:



      //take a 2d array and check if it is in _switch
      function checkSwitch(arr2d, rates, n = 0, found = ){
      if(n===arr2d.length){
      inCase(found, rates)
      break
      }
      for(let i in arr2d[n]){
      if(deepCheck(_switch, [...found, arr2d[n][i]]))
      checkSwitch(arr2d, rates, n+1, [...found, arr2d[n][i]])
      }
      return false
      }


      I have a 2d array (array of arrays) and I check if one element from each of the arrays is included as nested object keys in this _switch I am making. When I have an all match then run a function, and I want to continue checking the 2d array to see if there are other matches.



      However, if I ever come across an array with no matches in it then I need to break all iterations of the recursion for efficiency because I know there cannot logically be any matches of one from each.



      So I need to be able to break one instance and to break all of them. I am not sure how to obtain this.



      CORRECTED CODE AND LESSONS LEARNED
      I am amazed at how much I am learning about what intellignece actually is while working on artificial intelligence. In a recursive function adding in a return value will break the current instance and if you check the return when calling the recursion then you can passback the collapse of the entire recursion. Here is the new code:



      function checkSwitch(arr2d, rates, n = 0, found = ) {
      if (n === arr2d.length) {
      inCase(found, rates);
      return true;
      }
      let tf = false
      for (let i in arr2d[n]) {
      if (deepCheck(_switch, [...found, arr2d[n][i]])) {
      if (!checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
      return false;
      } else {
      tf = true
      }
      }
      }
      return tf;
      }


      Returning true means break this iteration and keep going, while returning false means break everything. I added in a toggle right before the for loop so that if we happen to get a match of one from every array, then logically we need to check every other possibility because they may be valid. otherwise if we get through the for without getting a true then break everything because there are no longer any possibilities.







      javascript arrays






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 4:56

























      asked Nov 20 '18 at 3:46







      user10302261































          3 Answers
          3






          active

          oldest

          votes


















          0














          You could simply use return instead, and always check the result from the caller's perspective. In this case you'll need to return a different value when it "breaks":



          function checkSwitch(arr2d, rates, n = 0, found = ) {
          if (n === arr2d.length) {
          inCase(found, rates);
          return true;
          }
          for (let i in arr2d[n]) {
          if (deepCheck(_switch, [...found, arr2d[n][i]])) {
          if (checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
          return true;
          }
          }
          }
          return false;
          }


          Here, returning true basically means "stop everything" (break out), and returning false means stop the current iteration normally (as OP explained himself in the comments below).



          This does not technically "break out", but will instead roll back up the call chain before returning, which is (imho) the cleanest approach (though technically not as optimal as breaking in one go) if you're going to stick to a recursive function.






          share|improve this answer


























          • The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).

            – user10302261
            Nov 20 '18 at 4:19











          • @CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The first false return value will stop the loop, as it will return back immediately. Refer to the if (!checkSwitch(...)) part.

            – Jeto
            Nov 20 '18 at 4:21













          • @CapitalJusticeWarrior The throw route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.

            – Jeto
            Nov 20 '18 at 4:23








          • 1





            my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!

            – user10302261
            Nov 20 '18 at 4:28











          • Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.

            – user10302261
            Nov 20 '18 at 4:34



















          1














          Neither. Loop control statements like break and continue are only valid inside a loop (for, while, do, or switch). The break statement in your code is not in a loop, so this code will cause a syntax error.



          The only way to "break out" of multiple levels of a recursive function is to throw an exception. This is probably not ideal for this situation; a better option for your situation may be to use a nested loop instead of recursion.






          share|improve this answer


























          • do I do a return then? maybe a continue?

            – user10302261
            Nov 20 '18 at 3:51











          • responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!

            – user10302261
            Nov 20 '18 at 3:57





















          0














          A break statement will terminate only the current loop, switch or label statement. It is the wrong tool to use for what you are trying to accomplish. (It should not be used at all where you have it, since it is outside one of those constructs.) A better approach is to have checkSwitch return a value indicating whether the matching process should be abandoned. That value can then percolate back up the recursion stack. Alternatively, throw an exception (which you will then have to catch somewhere).






          share|improve this answer
























          • Ok I will rewrite it with a return and a condition check then. Thank you!

            – user10302261
            Nov 20 '18 at 3:55













          • nvm I will try/catch an exception when I call it thanks! Way easier on my head!

            – user10302261
            Nov 20 '18 at 4:07











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385924%2fwill-javascript-break-stop-a-recursive-function-completely-or-just-that-instan%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown
























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You could simply use return instead, and always check the result from the caller's perspective. In this case you'll need to return a different value when it "breaks":



          function checkSwitch(arr2d, rates, n = 0, found = ) {
          if (n === arr2d.length) {
          inCase(found, rates);
          return true;
          }
          for (let i in arr2d[n]) {
          if (deepCheck(_switch, [...found, arr2d[n][i]])) {
          if (checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
          return true;
          }
          }
          }
          return false;
          }


          Here, returning true basically means "stop everything" (break out), and returning false means stop the current iteration normally (as OP explained himself in the comments below).



          This does not technically "break out", but will instead roll back up the call chain before returning, which is (imho) the cleanest approach (though technically not as optimal as breaking in one go) if you're going to stick to a recursive function.






          share|improve this answer


























          • The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).

            – user10302261
            Nov 20 '18 at 4:19











          • @CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The first false return value will stop the loop, as it will return back immediately. Refer to the if (!checkSwitch(...)) part.

            – Jeto
            Nov 20 '18 at 4:21













          • @CapitalJusticeWarrior The throw route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.

            – Jeto
            Nov 20 '18 at 4:23








          • 1





            my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!

            – user10302261
            Nov 20 '18 at 4:28











          • Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.

            – user10302261
            Nov 20 '18 at 4:34
















          0














          You could simply use return instead, and always check the result from the caller's perspective. In this case you'll need to return a different value when it "breaks":



          function checkSwitch(arr2d, rates, n = 0, found = ) {
          if (n === arr2d.length) {
          inCase(found, rates);
          return true;
          }
          for (let i in arr2d[n]) {
          if (deepCheck(_switch, [...found, arr2d[n][i]])) {
          if (checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
          return true;
          }
          }
          }
          return false;
          }


          Here, returning true basically means "stop everything" (break out), and returning false means stop the current iteration normally (as OP explained himself in the comments below).



          This does not technically "break out", but will instead roll back up the call chain before returning, which is (imho) the cleanest approach (though technically not as optimal as breaking in one go) if you're going to stick to a recursive function.






          share|improve this answer


























          • The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).

            – user10302261
            Nov 20 '18 at 4:19











          • @CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The first false return value will stop the loop, as it will return back immediately. Refer to the if (!checkSwitch(...)) part.

            – Jeto
            Nov 20 '18 at 4:21













          • @CapitalJusticeWarrior The throw route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.

            – Jeto
            Nov 20 '18 at 4:23








          • 1





            my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!

            – user10302261
            Nov 20 '18 at 4:28











          • Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.

            – user10302261
            Nov 20 '18 at 4:34














          0












          0








          0







          You could simply use return instead, and always check the result from the caller's perspective. In this case you'll need to return a different value when it "breaks":



          function checkSwitch(arr2d, rates, n = 0, found = ) {
          if (n === arr2d.length) {
          inCase(found, rates);
          return true;
          }
          for (let i in arr2d[n]) {
          if (deepCheck(_switch, [...found, arr2d[n][i]])) {
          if (checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
          return true;
          }
          }
          }
          return false;
          }


          Here, returning true basically means "stop everything" (break out), and returning false means stop the current iteration normally (as OP explained himself in the comments below).



          This does not technically "break out", but will instead roll back up the call chain before returning, which is (imho) the cleanest approach (though technically not as optimal as breaking in one go) if you're going to stick to a recursive function.






          share|improve this answer















          You could simply use return instead, and always check the result from the caller's perspective. In this case you'll need to return a different value when it "breaks":



          function checkSwitch(arr2d, rates, n = 0, found = ) {
          if (n === arr2d.length) {
          inCase(found, rates);
          return true;
          }
          for (let i in arr2d[n]) {
          if (deepCheck(_switch, [...found, arr2d[n][i]])) {
          if (checkSwitch(arr2d, rates, n + 1, [...found, arr2d[n][i]])) {
          return true;
          }
          }
          }
          return false;
          }


          Here, returning true basically means "stop everything" (break out), and returning false means stop the current iteration normally (as OP explained himself in the comments below).



          This does not technically "break out", but will instead roll back up the call chain before returning, which is (imho) the cleanest approach (though technically not as optimal as breaking in one go) if you're going to stick to a recursive function.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 4:38

























          answered Nov 20 '18 at 4:08









          JetoJeto

          5,77021119




          5,77021119













          • The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).

            – user10302261
            Nov 20 '18 at 4:19











          • @CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The first false return value will stop the loop, as it will return back immediately. Refer to the if (!checkSwitch(...)) part.

            – Jeto
            Nov 20 '18 at 4:21













          • @CapitalJusticeWarrior The throw route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.

            – Jeto
            Nov 20 '18 at 4:23








          • 1





            my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!

            – user10302261
            Nov 20 '18 at 4:28











          • Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.

            – user10302261
            Nov 20 '18 at 4:34



















          • The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).

            – user10302261
            Nov 20 '18 at 4:19











          • @CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The first false return value will stop the loop, as it will return back immediately. Refer to the if (!checkSwitch(...)) part.

            – Jeto
            Nov 20 '18 at 4:21













          • @CapitalJusticeWarrior The throw route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.

            – Jeto
            Nov 20 '18 at 4:23








          • 1





            my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!

            – user10302261
            Nov 20 '18 at 4:28











          • Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.

            – user10302261
            Nov 20 '18 at 4:34

















          The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).

          – user10302261
          Nov 20 '18 at 4:19





          The problem with this is that the for loops in prior instances will continue even after a later for loop has determined that there were no matches. The best route imo after seeing the other answers is to throw an exception and use a try/catch when the function is called (or better yet chain it in another function with the try catch block).

          – user10302261
          Nov 20 '18 at 4:19













          @CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The first false return value will stop the loop, as it will return back immediately. Refer to the if (!checkSwitch(...)) part.

          – Jeto
          Nov 20 '18 at 4:21







          @CapitalJusticeWarrior Nope, it won't continue, because every call's return value is checked. The first false return value will stop the loop, as it will return back immediately. Refer to the if (!checkSwitch(...)) part.

          – Jeto
          Nov 20 '18 at 4:21















          @CapitalJusticeWarrior The throw route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.

          – Jeto
          Nov 20 '18 at 4:23







          @CapitalJusticeWarrior The throw route will work and be efficient, but is kind of a gimmick. Use it only if a few milliseconds of performance is a major concern, imo.

          – Jeto
          Nov 20 '18 at 4:23






          1




          1





          my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!

          – user10302261
          Nov 20 '18 at 4:28





          my mistake. I will try it out and see. you may have the best answer in that case....ok I wanted true return on the first condition there but otherwise this checks out. Thank you. I'll give you the point!

          – user10302261
          Nov 20 '18 at 4:28













          Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.

          – user10302261
          Nov 20 '18 at 4:34





          Just to clarify for any body else reading this: returning true means stop this instance but keep going in others and returning false means stop everything. then all you have to do is check the return statement on your recursive call so it can be passed backward. I also agree with @jeto that making a try/catch block is not really that much more efficient even if you are doing very deep recursion and isn't really worth the extra mental effort therefore.

          – user10302261
          Nov 20 '18 at 4:34













          1














          Neither. Loop control statements like break and continue are only valid inside a loop (for, while, do, or switch). The break statement in your code is not in a loop, so this code will cause a syntax error.



          The only way to "break out" of multiple levels of a recursive function is to throw an exception. This is probably not ideal for this situation; a better option for your situation may be to use a nested loop instead of recursion.






          share|improve this answer


























          • do I do a return then? maybe a continue?

            – user10302261
            Nov 20 '18 at 3:51











          • responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!

            – user10302261
            Nov 20 '18 at 3:57


















          1














          Neither. Loop control statements like break and continue are only valid inside a loop (for, while, do, or switch). The break statement in your code is not in a loop, so this code will cause a syntax error.



          The only way to "break out" of multiple levels of a recursive function is to throw an exception. This is probably not ideal for this situation; a better option for your situation may be to use a nested loop instead of recursion.






          share|improve this answer


























          • do I do a return then? maybe a continue?

            – user10302261
            Nov 20 '18 at 3:51











          • responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!

            – user10302261
            Nov 20 '18 at 3:57
















          1












          1








          1







          Neither. Loop control statements like break and continue are only valid inside a loop (for, while, do, or switch). The break statement in your code is not in a loop, so this code will cause a syntax error.



          The only way to "break out" of multiple levels of a recursive function is to throw an exception. This is probably not ideal for this situation; a better option for your situation may be to use a nested loop instead of recursion.






          share|improve this answer















          Neither. Loop control statements like break and continue are only valid inside a loop (for, while, do, or switch). The break statement in your code is not in a loop, so this code will cause a syntax error.



          The only way to "break out" of multiple levels of a recursive function is to throw an exception. This is probably not ideal for this situation; a better option for your situation may be to use a nested loop instead of recursion.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 3:52

























          answered Nov 20 '18 at 3:50









          duskwuffduskwuff

          148k19177234




          148k19177234













          • do I do a return then? maybe a continue?

            – user10302261
            Nov 20 '18 at 3:51











          • responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!

            – user10302261
            Nov 20 '18 at 3:57





















          • do I do a return then? maybe a continue?

            – user10302261
            Nov 20 '18 at 3:51











          • responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!

            – user10302261
            Nov 20 '18 at 3:57



















          do I do a return then? maybe a continue?

          – user10302261
          Nov 20 '18 at 3:51





          do I do a return then? maybe a continue?

          – user10302261
          Nov 20 '18 at 3:51













          responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!

          – user10302261
          Nov 20 '18 at 3:57







          responding to edit: the problem with a nested loop however is that I need to save exactly the elements (one each) so that I can easily run the function without a computationally heavy combinatoric calculation to sort out what nested keys to check in my _switch object. EDIT: my _switch is a substitute for nested cases and man are there a lot of them!

          – user10302261
          Nov 20 '18 at 3:57













          0














          A break statement will terminate only the current loop, switch or label statement. It is the wrong tool to use for what you are trying to accomplish. (It should not be used at all where you have it, since it is outside one of those constructs.) A better approach is to have checkSwitch return a value indicating whether the matching process should be abandoned. That value can then percolate back up the recursion stack. Alternatively, throw an exception (which you will then have to catch somewhere).






          share|improve this answer
























          • Ok I will rewrite it with a return and a condition check then. Thank you!

            – user10302261
            Nov 20 '18 at 3:55













          • nvm I will try/catch an exception when I call it thanks! Way easier on my head!

            – user10302261
            Nov 20 '18 at 4:07
















          0














          A break statement will terminate only the current loop, switch or label statement. It is the wrong tool to use for what you are trying to accomplish. (It should not be used at all where you have it, since it is outside one of those constructs.) A better approach is to have checkSwitch return a value indicating whether the matching process should be abandoned. That value can then percolate back up the recursion stack. Alternatively, throw an exception (which you will then have to catch somewhere).






          share|improve this answer
























          • Ok I will rewrite it with a return and a condition check then. Thank you!

            – user10302261
            Nov 20 '18 at 3:55













          • nvm I will try/catch an exception when I call it thanks! Way easier on my head!

            – user10302261
            Nov 20 '18 at 4:07














          0












          0








          0







          A break statement will terminate only the current loop, switch or label statement. It is the wrong tool to use for what you are trying to accomplish. (It should not be used at all where you have it, since it is outside one of those constructs.) A better approach is to have checkSwitch return a value indicating whether the matching process should be abandoned. That value can then percolate back up the recursion stack. Alternatively, throw an exception (which you will then have to catch somewhere).






          share|improve this answer













          A break statement will terminate only the current loop, switch or label statement. It is the wrong tool to use for what you are trying to accomplish. (It should not be used at all where you have it, since it is outside one of those constructs.) A better approach is to have checkSwitch return a value indicating whether the matching process should be abandoned. That value can then percolate back up the recursion stack. Alternatively, throw an exception (which you will then have to catch somewhere).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 3:51









          Ted HoppTed Hopp

          200k41314426




          200k41314426













          • Ok I will rewrite it with a return and a condition check then. Thank you!

            – user10302261
            Nov 20 '18 at 3:55













          • nvm I will try/catch an exception when I call it thanks! Way easier on my head!

            – user10302261
            Nov 20 '18 at 4:07



















          • Ok I will rewrite it with a return and a condition check then. Thank you!

            – user10302261
            Nov 20 '18 at 3:55













          • nvm I will try/catch an exception when I call it thanks! Way easier on my head!

            – user10302261
            Nov 20 '18 at 4:07

















          Ok I will rewrite it with a return and a condition check then. Thank you!

          – user10302261
          Nov 20 '18 at 3:55







          Ok I will rewrite it with a return and a condition check then. Thank you!

          – user10302261
          Nov 20 '18 at 3:55















          nvm I will try/catch an exception when I call it thanks! Way easier on my head!

          – user10302261
          Nov 20 '18 at 4:07





          nvm I will try/catch an exception when I call it thanks! Way easier on my head!

          – user10302261
          Nov 20 '18 at 4:07


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385924%2fwill-javascript-break-stop-a-recursive-function-completely-or-just-that-instan%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          鏡平學校

          ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

          Why https connections are so slow when debugging (stepping over) in Java?