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
45
votes
11 answers

Nested jQuery.each() - continue/break

Consider the following code: var sentences = [ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'Vivamus aliquet nisl quis velit ornare tempor.', 'Cras sit amet neque ante, eu ultrices est.', 'Integer…
Ryan Kinal
  • 17,414
  • 6
  • 46
  • 63
44
votes
6 answers

How do exit two nested loops?

I have been using Java for quite some time, yet my education in loops is somewhat lacking. I know how to create every loop that exists in java and break out of the loops as well. However, I've recently thought about this: Say I have two nested…
fireshadow52
  • 6,298
  • 2
  • 30
  • 46
42
votes
4 answers

In Java, how does break interact with nested loops?

I know a break statement jumps out of a loop, but does it jump out of nested loops or just the one its currently in?
Craig
  • 421
  • 1
  • 4
  • 3
42
votes
6 answers

Break long word with CSS

I have a situation where there can be long words like 'hellowordsometext' or integer like '1234567891122' without any space in between. check this js please. http://jsfiddle.net/rzq5e/6/ how is it possible to break it in to next line after it reach…
dev1234
  • 5,376
  • 15
  • 56
  • 115
41
votes
1 answer

is there a equivalent of Java's labelled break in C# or a workaround

I am converting some Java code to C# and have found a few labelled "break" statements (e.g.) label1: while (somethingA) { ... while (somethingB) { if (condition) { break label1; } } …
peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
40
votes
4 answers

Break out out forloop but within switch statement php

When I normally want to break out of a foreach loop before all of the iterations have completed I simply use a break; statement. e.g. foreach($nodelist as $node) { if($metCriteria) { break; } } But my next example has a switch…
Lizard
  • 43,732
  • 39
  • 106
  • 167
39
votes
4 answers

Python - `break` out of all loops

I am using multiple nested for loops. In the last loop there is an if statement. When evaluated to True all the for loops should stop, but that does not happen. It only breaks out of the innermost for loop, and than it keeps on going. I need all…
Vader
  • 6,335
  • 8
  • 31
  • 43
39
votes
6 answers

How do I break from the main/outer loop in a double/nested loop?

If I have loop in a loop and once an if statement is satisfied I want to break main loop, how am I supposed to do that? This is my code: for (int d = 0; d < amountOfNeighbors; d++) { for (int c = 0; c < myArray.size(); c++) { if…
Shepard
  • 1,111
  • 5
  • 16
  • 32
38
votes
9 answers

Exiting out of a FOR loop in a batch file?

Why does this batch file never break out of the loop? For /L %%f In (1,1,1000000) Do @If Not Exist %%f Goto :EOF Shouldn't the Goto :EOF break out of the loop? Edit: I guess I should've asked more explicitly... how can I break out of the loop?
user541686
  • 205,094
  • 128
  • 528
  • 886
38
votes
3 answers

How to properly break out of a promise chain?

Based on the question here: jQuery chaining and cascading then's and when's and the accepted answer, I want to break the promise chain at a point but haven't yet found the correct way. There are multiple posts about this, but I am still lost. Taking…
Dennis G
  • 21,405
  • 19
  • 96
  • 133
38
votes
5 answers

How to break out of while loop in Python?

I have to make this game for my comp class, and I can't figure out how how break out of this loop. See, I have to play against the "computer," by rolling bigger numbers, and seeing who has the bigger score. But I can't figure out how to "break" from…
Ninja
  • 379
  • 1
  • 3
  • 4
37
votes
2 answers

BREAK statement in PL/pgSQL

How to have the break statement in PostgreSQL? I have the structure like this: for() { for() { if(somecondition) break; } } As per my understanding it should only break the inner for loop?
user1844840
  • 1,937
  • 3
  • 16
  • 13
35
votes
6 answers

Does `break` work only for `for`, `while`, `do-while`, `switch' and for `if` statements?

Suppose, I have a if statement inside a for loop: for( ; ; ) { if( ) { printf(" inside if"); break; }//if printf("inside for"); }//for Now, will the break statement cause the compiler to come out of the for loop or…
avi
  • 1,847
  • 3
  • 16
  • 17
34
votes
4 answers

How do I get GDB to break out of a loop?

I can tell GDB to return from a function immediately with return, and call a function with call myFunction. But how do I get it break out of the current loop? i.e. to act as if it's hit a break; statement. Is jump myfile.c: the way to…
John Carter
  • 53,924
  • 26
  • 111
  • 144
33
votes
4 answers

break and return in ruby, how do you use them?

I just asked a question about return and it seems to do the same thing as break. How do you use return, and how do you use break, such as in the actual code that you write to solve the problems that can use these constructs. I can't really post…
thenengah
  • 42,557
  • 33
  • 113
  • 157