Python: 'break' outside loop










25















in the following python code:



narg=len(sys.argv)
print "@length arg= ", narg
if narg == 1:
print "@Usage: input_filename nelements nintervals"
break


I get:



SyntaxError: 'break' outside loop


Why?










share|improve this question



















  • 4





    What do you think break does here? What do you expect to happen?

    – S.Lott
    Mar 17 '10 at 13:35







  • 2





    The optparse module (or similar) will make parsing argv much simpler. docs.python.org/library/optparse.html

    – Roger Pate
    Mar 17 '10 at 13:36















25















in the following python code:



narg=len(sys.argv)
print "@length arg= ", narg
if narg == 1:
print "@Usage: input_filename nelements nintervals"
break


I get:



SyntaxError: 'break' outside loop


Why?










share|improve this question



















  • 4





    What do you think break does here? What do you expect to happen?

    – S.Lott
    Mar 17 '10 at 13:35







  • 2





    The optparse module (or similar) will make parsing argv much simpler. docs.python.org/library/optparse.html

    – Roger Pate
    Mar 17 '10 at 13:36













25












25








25


10






in the following python code:



narg=len(sys.argv)
print "@length arg= ", narg
if narg == 1:
print "@Usage: input_filename nelements nintervals"
break


I get:



SyntaxError: 'break' outside loop


Why?










share|improve this question
















in the following python code:



narg=len(sys.argv)
print "@length arg= ", narg
if narg == 1:
print "@Usage: input_filename nelements nintervals"
break


I get:



SyntaxError: 'break' outside loop


Why?







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 4 '16 at 11:45









Serenity

18.8k115572




18.8k115572










asked Mar 17 '10 at 13:32









Open the wayOpen the way

7,15238115179




7,15238115179







  • 4





    What do you think break does here? What do you expect to happen?

    – S.Lott
    Mar 17 '10 at 13:35







  • 2





    The optparse module (or similar) will make parsing argv much simpler. docs.python.org/library/optparse.html

    – Roger Pate
    Mar 17 '10 at 13:36












  • 4





    What do you think break does here? What do you expect to happen?

    – S.Lott
    Mar 17 '10 at 13:35







  • 2





    The optparse module (or similar) will make parsing argv much simpler. docs.python.org/library/optparse.html

    – Roger Pate
    Mar 17 '10 at 13:36







4




4





What do you think break does here? What do you expect to happen?

– S.Lott
Mar 17 '10 at 13:35






What do you think break does here? What do you expect to happen?

– S.Lott
Mar 17 '10 at 13:35





2




2





The optparse module (or similar) will make parsing argv much simpler. docs.python.org/library/optparse.html

– Roger Pate
Mar 17 '10 at 13:36





The optparse module (or similar) will make parsing argv much simpler. docs.python.org/library/optparse.html

– Roger Pate
Mar 17 '10 at 13:36












4 Answers
4






active

oldest

votes


















48














Because break cannot be used to break out of an if - it can only break out of loops. That's the way Python (and most other languages) are specified to behave.



What are you trying to do? Perhaps you should use sys.exit() or return instead?






share|improve this answer


















  • 1





    and how could I do so the scripts stops here?

    – Open the way
    Mar 17 '10 at 13:35






  • 6





    sys.exit() terminates the program immediately.

    – Mark Byers
    Mar 17 '10 at 13:36






  • 2





    raise SystemExit terminates the program; sys.exit() raises it, if you prefer a function call.

    – tzot
    Apr 4 '10 at 8:24


















2














Because the break statement is intended to break out of loops. You don't need to break out of an if statement - it just ends at the end.






share|improve this answer






























    2














    Because break can only be used inside a loop.
    It is used to break out of a loop (stop the loop).






    share|improve this answer






























      1














      break breaks out of a loop, not an if statement, as others have pointed out. The motivation for this isn't too hard to see; think about code like



      for item in some_iterable:
      ...
      if break_condition():
      break


      The break would be pretty useless if it terminated the if block rather than terminated the loop -- terminating a loop conditionally is the exact thing break is used for.






      share|improve this answer

























      • Is your last sentence equivalent to "A break should only be used directly inside a loop."?

        – Quetzalcoatl
        Nov 12 '18 at 2:42












      • @Quetzalcoatl I don't think so. What do you mean "directly"?

        – Mike Graham
        Nov 14 '18 at 6:17











      • a break in a loop, where this break is not under a condition. the code in your response is a case where break is not "directly" in the loop -- it is inside of a condition. somehow your last sentence was grammatically unclear to me :-)

        – Quetzalcoatl
        Nov 15 '18 at 0:18







      • 1





        @Quetzalcoatl I rephrased -- I was trying to say that you would never put break directly inside a loop. You always put it inside an if inside a loop.

        – Mike Graham
        Nov 15 '18 at 5:28











      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%2f2462566%2fpython-break-outside-loop%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      48














      Because break cannot be used to break out of an if - it can only break out of loops. That's the way Python (and most other languages) are specified to behave.



      What are you trying to do? Perhaps you should use sys.exit() or return instead?






      share|improve this answer


















      • 1





        and how could I do so the scripts stops here?

        – Open the way
        Mar 17 '10 at 13:35






      • 6





        sys.exit() terminates the program immediately.

        – Mark Byers
        Mar 17 '10 at 13:36






      • 2





        raise SystemExit terminates the program; sys.exit() raises it, if you prefer a function call.

        – tzot
        Apr 4 '10 at 8:24















      48














      Because break cannot be used to break out of an if - it can only break out of loops. That's the way Python (and most other languages) are specified to behave.



      What are you trying to do? Perhaps you should use sys.exit() or return instead?






      share|improve this answer


















      • 1





        and how could I do so the scripts stops here?

        – Open the way
        Mar 17 '10 at 13:35






      • 6





        sys.exit() terminates the program immediately.

        – Mark Byers
        Mar 17 '10 at 13:36






      • 2





        raise SystemExit terminates the program; sys.exit() raises it, if you prefer a function call.

        – tzot
        Apr 4 '10 at 8:24













      48












      48








      48







      Because break cannot be used to break out of an if - it can only break out of loops. That's the way Python (and most other languages) are specified to behave.



      What are you trying to do? Perhaps you should use sys.exit() or return instead?






      share|improve this answer













      Because break cannot be used to break out of an if - it can only break out of loops. That's the way Python (and most other languages) are specified to behave.



      What are you trying to do? Perhaps you should use sys.exit() or return instead?







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 17 '10 at 13:34









      Mark ByersMark Byers

      598k12713701348




      598k12713701348







      • 1





        and how could I do so the scripts stops here?

        – Open the way
        Mar 17 '10 at 13:35






      • 6





        sys.exit() terminates the program immediately.

        – Mark Byers
        Mar 17 '10 at 13:36






      • 2





        raise SystemExit terminates the program; sys.exit() raises it, if you prefer a function call.

        – tzot
        Apr 4 '10 at 8:24












      • 1





        and how could I do so the scripts stops here?

        – Open the way
        Mar 17 '10 at 13:35






      • 6





        sys.exit() terminates the program immediately.

        – Mark Byers
        Mar 17 '10 at 13:36






      • 2





        raise SystemExit terminates the program; sys.exit() raises it, if you prefer a function call.

        – tzot
        Apr 4 '10 at 8:24







      1




      1





      and how could I do so the scripts stops here?

      – Open the way
      Mar 17 '10 at 13:35





      and how could I do so the scripts stops here?

      – Open the way
      Mar 17 '10 at 13:35




      6




      6





      sys.exit() terminates the program immediately.

      – Mark Byers
      Mar 17 '10 at 13:36





      sys.exit() terminates the program immediately.

      – Mark Byers
      Mar 17 '10 at 13:36




      2




      2





      raise SystemExit terminates the program; sys.exit() raises it, if you prefer a function call.

      – tzot
      Apr 4 '10 at 8:24





      raise SystemExit terminates the program; sys.exit() raises it, if you prefer a function call.

      – tzot
      Apr 4 '10 at 8:24













      2














      Because the break statement is intended to break out of loops. You don't need to break out of an if statement - it just ends at the end.






      share|improve this answer



























        2














        Because the break statement is intended to break out of loops. You don't need to break out of an if statement - it just ends at the end.






        share|improve this answer

























          2












          2








          2







          Because the break statement is intended to break out of loops. You don't need to break out of an if statement - it just ends at the end.






          share|improve this answer













          Because the break statement is intended to break out of loops. You don't need to break out of an if statement - it just ends at the end.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 17 '10 at 13:35









          Steve314Steve314

          21.8k1049110




          21.8k1049110





















              2














              Because break can only be used inside a loop.
              It is used to break out of a loop (stop the loop).






              share|improve this answer



























                2














                Because break can only be used inside a loop.
                It is used to break out of a loop (stop the loop).






                share|improve this answer

























                  2












                  2








                  2







                  Because break can only be used inside a loop.
                  It is used to break out of a loop (stop the loop).






                  share|improve this answer













                  Because break can only be used inside a loop.
                  It is used to break out of a loop (stop the loop).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 17 '10 at 13:35









                  enricogenricog

                  2,86052746




                  2,86052746





















                      1














                      break breaks out of a loop, not an if statement, as others have pointed out. The motivation for this isn't too hard to see; think about code like



                      for item in some_iterable:
                      ...
                      if break_condition():
                      break


                      The break would be pretty useless if it terminated the if block rather than terminated the loop -- terminating a loop conditionally is the exact thing break is used for.






                      share|improve this answer

























                      • Is your last sentence equivalent to "A break should only be used directly inside a loop."?

                        – Quetzalcoatl
                        Nov 12 '18 at 2:42












                      • @Quetzalcoatl I don't think so. What do you mean "directly"?

                        – Mike Graham
                        Nov 14 '18 at 6:17











                      • a break in a loop, where this break is not under a condition. the code in your response is a case where break is not "directly" in the loop -- it is inside of a condition. somehow your last sentence was grammatically unclear to me :-)

                        – Quetzalcoatl
                        Nov 15 '18 at 0:18







                      • 1





                        @Quetzalcoatl I rephrased -- I was trying to say that you would never put break directly inside a loop. You always put it inside an if inside a loop.

                        – Mike Graham
                        Nov 15 '18 at 5:28















                      1














                      break breaks out of a loop, not an if statement, as others have pointed out. The motivation for this isn't too hard to see; think about code like



                      for item in some_iterable:
                      ...
                      if break_condition():
                      break


                      The break would be pretty useless if it terminated the if block rather than terminated the loop -- terminating a loop conditionally is the exact thing break is used for.






                      share|improve this answer

























                      • Is your last sentence equivalent to "A break should only be used directly inside a loop."?

                        – Quetzalcoatl
                        Nov 12 '18 at 2:42












                      • @Quetzalcoatl I don't think so. What do you mean "directly"?

                        – Mike Graham
                        Nov 14 '18 at 6:17











                      • a break in a loop, where this break is not under a condition. the code in your response is a case where break is not "directly" in the loop -- it is inside of a condition. somehow your last sentence was grammatically unclear to me :-)

                        – Quetzalcoatl
                        Nov 15 '18 at 0:18







                      • 1





                        @Quetzalcoatl I rephrased -- I was trying to say that you would never put break directly inside a loop. You always put it inside an if inside a loop.

                        – Mike Graham
                        Nov 15 '18 at 5:28













                      1












                      1








                      1







                      break breaks out of a loop, not an if statement, as others have pointed out. The motivation for this isn't too hard to see; think about code like



                      for item in some_iterable:
                      ...
                      if break_condition():
                      break


                      The break would be pretty useless if it terminated the if block rather than terminated the loop -- terminating a loop conditionally is the exact thing break is used for.






                      share|improve this answer















                      break breaks out of a loop, not an if statement, as others have pointed out. The motivation for this isn't too hard to see; think about code like



                      for item in some_iterable:
                      ...
                      if break_condition():
                      break


                      The break would be pretty useless if it terminated the if block rather than terminated the loop -- terminating a loop conditionally is the exact thing break is used for.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 15 '18 at 5:27

























                      answered Mar 17 '10 at 16:17









                      Mike GrahamMike Graham

                      51.4k1076116




                      51.4k1076116












                      • Is your last sentence equivalent to "A break should only be used directly inside a loop."?

                        – Quetzalcoatl
                        Nov 12 '18 at 2:42












                      • @Quetzalcoatl I don't think so. What do you mean "directly"?

                        – Mike Graham
                        Nov 14 '18 at 6:17











                      • a break in a loop, where this break is not under a condition. the code in your response is a case where break is not "directly" in the loop -- it is inside of a condition. somehow your last sentence was grammatically unclear to me :-)

                        – Quetzalcoatl
                        Nov 15 '18 at 0:18







                      • 1





                        @Quetzalcoatl I rephrased -- I was trying to say that you would never put break directly inside a loop. You always put it inside an if inside a loop.

                        – Mike Graham
                        Nov 15 '18 at 5:28

















                      • Is your last sentence equivalent to "A break should only be used directly inside a loop."?

                        – Quetzalcoatl
                        Nov 12 '18 at 2:42












                      • @Quetzalcoatl I don't think so. What do you mean "directly"?

                        – Mike Graham
                        Nov 14 '18 at 6:17











                      • a break in a loop, where this break is not under a condition. the code in your response is a case where break is not "directly" in the loop -- it is inside of a condition. somehow your last sentence was grammatically unclear to me :-)

                        – Quetzalcoatl
                        Nov 15 '18 at 0:18







                      • 1





                        @Quetzalcoatl I rephrased -- I was trying to say that you would never put break directly inside a loop. You always put it inside an if inside a loop.

                        – Mike Graham
                        Nov 15 '18 at 5:28
















                      Is your last sentence equivalent to "A break should only be used directly inside a loop."?

                      – Quetzalcoatl
                      Nov 12 '18 at 2:42






                      Is your last sentence equivalent to "A break should only be used directly inside a loop."?

                      – Quetzalcoatl
                      Nov 12 '18 at 2:42














                      @Quetzalcoatl I don't think so. What do you mean "directly"?

                      – Mike Graham
                      Nov 14 '18 at 6:17





                      @Quetzalcoatl I don't think so. What do you mean "directly"?

                      – Mike Graham
                      Nov 14 '18 at 6:17













                      a break in a loop, where this break is not under a condition. the code in your response is a case where break is not "directly" in the loop -- it is inside of a condition. somehow your last sentence was grammatically unclear to me :-)

                      – Quetzalcoatl
                      Nov 15 '18 at 0:18






                      a break in a loop, where this break is not under a condition. the code in your response is a case where break is not "directly" in the loop -- it is inside of a condition. somehow your last sentence was grammatically unclear to me :-)

                      – Quetzalcoatl
                      Nov 15 '18 at 0:18





                      1




                      1





                      @Quetzalcoatl I rephrased -- I was trying to say that you would never put break directly inside a loop. You always put it inside an if inside a loop.

                      – Mike Graham
                      Nov 15 '18 at 5:28





                      @Quetzalcoatl I rephrased -- I was trying to say that you would never put break directly inside a loop. You always put it inside an if inside a loop.

                      – Mike Graham
                      Nov 15 '18 at 5:28

















                      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%2f2462566%2fpython-break-outside-loop%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

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Darth Vader #20

                      Ondo