Python: 'break' outside loop
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
add a comment |
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
4
What do you thinkbreak
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
add a comment |
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
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
python
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 thinkbreak
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
add a comment |
4
What do you thinkbreak
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
add a comment |
4 Answers
4
active
oldest
votes
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?
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
add a comment |
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.
add a comment |
Because break can only be used inside a loop.
It is used to break out of a loop (stop the loop).
add a comment |
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.
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 putbreak
directly inside a loop. You always put it inside anif
inside a loop.
– Mike Graham
Nov 15 '18 at 5:28
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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?
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
add a comment |
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?
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
add a comment |
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?
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?
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 17 '10 at 13:35
Steve314Steve314
21.8k1049110
21.8k1049110
add a comment |
add a comment |
Because break can only be used inside a loop.
It is used to break out of a loop (stop the loop).
add a comment |
Because break can only be used inside a loop.
It is used to break out of a loop (stop the loop).
add a comment |
Because break can only be used inside a loop.
It is used to break out of a loop (stop the loop).
Because break can only be used inside a loop.
It is used to break out of a loop (stop the loop).
answered Mar 17 '10 at 13:35
enricogenricog
2,86052746
2,86052746
add a comment |
add a comment |
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.
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 putbreak
directly inside a loop. You always put it inside anif
inside a loop.
– Mike Graham
Nov 15 '18 at 5:28
add a comment |
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.
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 putbreak
directly inside a loop. You always put it inside anif
inside a loop.
– Mike Graham
Nov 15 '18 at 5:28
add a comment |
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.
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.
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 putbreak
directly inside a loop. You always put it inside anif
inside a loop.
– Mike Graham
Nov 15 '18 at 5:28
add a comment |
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 putbreak
directly inside a loop. You always put it inside anif
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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