Questions tagged [break]

A break statement is a flow-control feature provided by most programming languages that allows for an early exit from a loop; once a break statement is reached, its enclosing loop is immediately exited.

Using break will immediately exit the loop without completing the current iteration, in this example when i is 3 the loop will finish without any other line in the loop being executed.

Example (Python):

for i in range(1, 6):
    print(i)
    if i == 3:
        break
    print("do stuff")
print("after the loop")

Output:

1
do stuff
2
do stuff
3
after the loop

For more information see

2598 questions
25
votes
9 answers

ForEach() : Why can't use break/continue inside

Since ForEach() method loop through all list members, Why can't I use a break/continue clause while I can use them inside a normal foreach loop lstTemp.ForEach(i=> { if (i == 3) break; //do sth } ); Error: "No enclosing loop out of…
Rami Alshareef
  • 7,015
  • 12
  • 47
  • 75
25
votes
2 answers

Python Return error from function

I am writing a python function which uses two arrays of equal size [n,1]. Before performing any calculations, I'd like to check to ensure the lengths are the same, and if not, return an error. What is the best practice? def…
GPB
  • 2,395
  • 8
  • 26
  • 36
25
votes
3 answers

Why is break required after yield return in a switch statement?

Can somebody tell me why compiler thinks that break is necessary after yield return in the following code? foreach (DesignerNode node in nodeProvider.GetNodes(span, node => node.NodeType != NDjango.Interfaces.NodeType.ParsingContext)) { switch…
mfeingold
  • 7,094
  • 4
  • 37
  • 43
25
votes
2 answers

how to break out of only one nested loop

I have two tab-delimited files, and I need to test every row in the first file against all the rows in the other file. For instance, file1: row1 c1 36 345 A row2 c3 36 9949 B row3 c4 36 858 C file2: row1 c1 …
biohazard
  • 2,017
  • 10
  • 28
  • 41
24
votes
1 answer

Break for loop in an if statement

Currently having trouble with breaking this for loop. I want to break it if the variable is not found in this list so it can move two another for loop. It expects an indented block for the top of the for loop, but if I change the position of the…
mansa
  • 271
  • 1
  • 2
  • 6
24
votes
2 answers

How do I break an outer loop from an inner one in Perl?

Suppose I have a piece of Perl code like: foreach my $x (@x) { foreach my $y (@z) { foreach my $z (@z) { if (something()) { # I want to break free! } # do stuff } # do stuff } # do stuff } If something() is true, I would like…
David B
  • 29,258
  • 50
  • 133
  • 186
24
votes
2 answers

How to cleanly shut down a console app started with Process.Start?

This is looking like an impossible task. Absolutely nothing I've found works. The question is how to cleanly close a console application started with Process.Start that has been started with no console window and without using shell execute:…
Triynko
  • 18,766
  • 21
  • 107
  • 173
24
votes
6 answers

Is there any way to break out of a foreach loop?

I am using the R package foreach() with %dopar% to do long (~days) calculations in parallel. I would like the ability to stop the entire set of calculations in the event that one of them produces an error. However, I have not found a way to achieve…
Coryn Bailer-Jones
  • 289
  • 1
  • 2
  • 7
24
votes
4 answers

break and continue in function

def funcA(i): if i%3==0: print "Oh! No!", print i break for i in range(100): funcA(i) print "Pass", print i I know script above won't work. So, how can I write if I need put a function with break or continue into a…
ThunderEX
  • 703
  • 1
  • 7
  • 13
23
votes
6 answers

Do I have to break after throwing exception?

I'm writing a custom class in C# and I'm throwing a couple exceptions if people give the wrong inputs in some of the methods. If the exception is thrown, will any of the code in the method after the throw still be executed? Do I have to put a break…
Ross
  • 1,553
  • 1
  • 14
  • 22
23
votes
9 answers

Breaking a "for" loop using "break" considered harmful?

Some days ago I started a quick open source project and, when some mates looked at the code on svn, one of them told me that using break statement inside a for loop is considered harmful and shouldn't be done. He added, though, that I would find…
José Tomás Tocino
  • 9,873
  • 5
  • 44
  • 78
23
votes
3 answers

Is there a way to break from an array's reduce function in Swift?

Is there a way to do something similar to break from a for loop but in array's reduce() function? E.g. consider I have an array: var flags = [false, false, true, false, false, true, false] ... and I need to get a cumulative || on them. With a for…
0x416e746f6e
  • 9,872
  • 5
  • 40
  • 68
23
votes
3 answers

How to break a while loop from an if condition inside the while loop?

I want to break a while loop of the format below which has an if statement. If that if statement is true, the while loop also must break. Any help would be appreciated. while(something.hasnext()) { do something... if(contains something to…
SuperCoder
  • 262
  • 1
  • 3
  • 13
23
votes
4 answers

Java: break statement in "if else"

I keep getting an error, if without else. I tried else if as well for (;;) { System.out.println("---> Your choice: "); choice = input.nextInt(); if (choice==1) playGame(); if (choice==2) loadGame(); if…
John
  • 2,015
  • 5
  • 23
  • 37
22
votes
2 answers

How to check whether for loop ends completely in python?

This is a ceased for loop : for i in [1,2,3]: print(i) if i==3: break How can I check its difference with this : for i in [1,2,3]: print(i) This is an idea : IsBroken=False for i in [1,2,3]: print(i) if i==3: …
Jona
  • 629
  • 1
  • 6
  • 16