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
837
votes
7 answers

How to break out of jQuery each loop?

How do I break out of a jQuery each loop? I have tried: return false; in the loop but this did not work. Any ideas? Update 9/5/2020 I put the return false; in the wrong place. When I put it inside the loop everything worked.
Luke101
  • 63,072
  • 85
  • 231
  • 359
713
votes
39 answers

How can I break out of multiple loops?

Given the following code (that doesn't work): while True: # Snip: print out current state while True: ok = get_input("Is this ok? (y/n)") if ok.lower() == "y": break 2 # This doesn't work :( if ok.lower() == "n":…
Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
630
votes
18 answers

What's the best way to break from nested loops in JavaScript?

What's the best way to break from nested loops in Javascript? //Write the links to the page. for (var x = 0; x < Args.length; x++) { for (var Heading in Navigation.Headings) { for (var Item in Navigation.Headings[Heading]) { …
Gary Willoughby
  • 50,926
  • 41
  • 133
  • 199
410
votes
24 answers

Can I use break to exit multiple nested 'for' loops?

Is it possible to use the break function to exit several nested for loops? If so, how would you go about doing this? Can you also control how many loops the break exits?
Faken
  • 11,352
  • 17
  • 59
  • 74
346
votes
4 answers

break out of if and foreach

I have a foreach loop and an if statement. If a match is found i need to ultimately break out of the foreach. foreach ($equipxml as $equip) { $current_device = $equip->xpath("name"); if ($current_device[0] == $device) { // Found a…
au_stan
  • 4,011
  • 2
  • 19
  • 24
321
votes
5 answers

How do I break out of a loop in Perl?

I'm trying to use a break statement in a for loop, but since I'm also using strict subs in my Perl code, I'm getting an error saying: Bareword "break" not allowed while "strict subs" in use at ./final.pl line 154. Is there a workaround for…
Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133
309
votes
19 answers

How do I break out of a loop in Scala?

How do I break out a loop? var largest=0 for(i<-999 to 1 by -1) { for (j<-i to 1 by -1) { val product=i*j if (largest>product) // I want to break out here else …
TiansHUo
  • 8,509
  • 7
  • 45
  • 57
297
votes
21 answers

Difference between break and continue statement

Can anyone tell me the difference between break and continue statements?
DonX
  • 16,093
  • 21
  • 75
  • 120
281
votes
3 answers

Is returning out of a switch statement considered a better practice than using break?

Option 1 - switch using return: function myFunction(opt) { switch (opt) { case 1: return "One"; case 2: return "Two"; case 3: return "Three"; default: return ""; } } Option 2 - switch using break: function myFunction(opt)…
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
249
votes
14 answers

How to break nested loops in JavaScript?

I tried this: for(i = 0; i < 5; i++){ for(j = i + 1; j < 5; j++){ break(2); } alert(1); } only to get: SyntaxError: missing ; before statement So, how would I break a nested loop in JavaScript?
Mask
  • 33,129
  • 48
  • 101
  • 125
211
votes
7 answers

Does a break statement break from a switch/select?

I know that switch/select statements break automatically after every case. I am wondering, in the following code: for { switch sometest() { case 0: dosomething() case 1: break default: dosomethingelse() …
Matt
  • 21,026
  • 18
  • 63
  • 115
171
votes
7 answers

How to break out or exit a method in Java?

The keyword break in Java can be used for breaking out of a loop or switch statement. Is there anything which can be used to break from a method?
CodeMonkey
  • 2,511
  • 4
  • 27
  • 38
159
votes
20 answers

How to break out of a loop from inside a switch?

I'm writing some code that looks like this: while(true) { switch(msg->state) { case MSGTYPE: // ... break; // ... more stuff ... case DONE: break; // **HERE, I want to break out of the loop itself** } } Is there…
jrharshath
  • 25,975
  • 33
  • 97
  • 127
154
votes
19 answers

Is it a bad practice to use break in a for loop?

Is it a bad practice to use break statement inside a for loop? Say, I am searching for an value in an array. Compare inside a for loop and when value is found, break; to exit the for loop. Is this a bad practice? I have seen the alternative used:…
kiki
  • 13,627
  • 17
  • 49
  • 62
146
votes
6 answers

How can I break an outer loop with PHP?

I am looking to break an outer for/foreach loop in PHP. This can be done in ActionScript like so: top : for each(var i:MovieClip in movieClipArray) { for each(var j:String in nameArray) { if(i.name == j) break top; } } What's…
Marty
  • 39,033
  • 19
  • 93
  • 162
1
2 3
99 100