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
79
votes
5 answers

Line Break in XML formatting?

when editing a String in XML I need to add line breaks. And I wanted to ask what is the RIGHT form when programming for android? Because
works but ECLIPSE marks the area as problematic. If I check out suggestions Eclipse tells me that I shall…
Oliver Goossens
  • 1,903
  • 3
  • 20
  • 26
76
votes
5 answers

How to break out of multiple loops at once in C#?

What if I have nested loops, and I want to break out of all of them at once? while (true) { // ... while (shouldCont) { // ... while (shouldGo) { // ... if (timeToStop) { break; //…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
76
votes
4 answers

Java How can I break a while loop under a switch statement?

I have a homework to implement a simple testing application, below is my current code: import java.util.*; public class Test{ private static int typing; public static void main(String argv[]){ Scanner sc = new Scanner(System.in); …
user3394598
74
votes
22 answers

Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code)

Visual Studio 2017 breaks in debug mode and displays the message: Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code). The message is in the…
Gerard
  • 13,023
  • 14
  • 72
  • 125
73
votes
11 answers

Regarding Java switch statements - using return and omitting breaks in each case

Given this method, does this represent some egregious stylistic or semantic faux pas: private double translateSlider(int sliderVal) { switch (sliderVal) { case 0: return 1.0; case 1: return .9; …
NickAbbey
  • 1,201
  • 2
  • 10
  • 17
71
votes
15 answers

Break out of a while loop that contains a switch statement

I am having trouble figuring out how to break out of a loop that contains a switch statement. Break breaks out of the switch, not the loop. There is probably a more elegant solution to this. I have implemented a flag that starts out as true and…
joshblair
  • 877
  • 1
  • 6
  • 14
69
votes
2 answers

How do I do a "break" or "continue" when in a functional loop within Kotlin?

In Kotlin, I cannot do a break or continue within a function loop and my lambda -- like I can from a normal for loop. For example, this does not work: (1..5).forEach { continue@forEach // not allowed, nor break@forEach } There are old…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
67
votes
2 answers

R: Break for loop

Can you confirm if the next break cancels the inner for loop? for (out in 1:n_old){ id_velho <- old_table_df$id[out] for (in in 1:n) { id_novo <- new_table_df$ID[in] if(id_velho==id_novo) { break …
Rui Morais
  • 689
  • 1
  • 6
  • 6
67
votes
8 answers

Is using a 'goto' statement bad?

After doing some reseach on how to break through a secondary loop while (true) { // Main Loop for (int I = 0; I < 15; I++) { // Secondary loop // Do Something break; // Break main loop? } } most people recommended to call the…
Rasmus Søborg
  • 3,597
  • 4
  • 29
  • 46
59
votes
5 answers

What does a semicolon do?

I got a function online to help me with my current project and it had semicolons on some of the lines. I was wondering why? Is it to break the function? def containsAny(self, strings=[]): alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789' for…
Brandon Nadeau
  • 3,568
  • 13
  • 42
  • 65
57
votes
5 answers

Is using labels in JavaScript bad practice?

I just found out about using label s in JavaScript, such as: for (var i in team) { if(i === "something") { break doThis: //Goto the label } else { doThat(); } } doThis: //Label doIt(); I've not heard about this until…
Ryan
  • 3,726
  • 3
  • 26
  • 38
57
votes
1 answer

Named breaks in for loops in Rust

Is there a way to have nested for loops in Rust and break the outer one from inside the inner one, the way one could do e.g. in Java? I know Rust supports named breaks in loop but I can't seem to find information about the same regarding for.
Arets Paeglis
  • 3,856
  • 4
  • 35
  • 44
56
votes
4 answers

What is meant by a number after "break" or "continue" in PHP?

Could someone please explain, with examples, what is meant by loop break 2 or continue 2 in PHP? What does it mean when break or continue is followed by a number?
tree em
  • 20,379
  • 30
  • 92
  • 130
55
votes
7 answers

Should we break the default case in switch statement?

Assuming this example code (source): #include void playgame() { printf( "Play game called" ); } void loadgame() { printf( "Load game called" ); } void playmultiplayer() { printf( "Play multiplayer game called" ); } int…
gsamaras
  • 71,951
  • 46
  • 188
  • 305
55
votes
5 answers

illegal use of break statement; javascript

When this variable becomes a certain amount i want the loop to stop, but i keep getting the error, "Uncaught SyntaxError: Illegal break statement". function loop() { if (isPlaying) { jet1.draw(); drawAllEnemies(); …
lehermj
  • 916
  • 4
  • 9
  • 20