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
138
votes
20 answers

HTML5 canvas ctx.fillText won't do line breaks?

I can't seem to be able to add text to a canvas if the text includes "\n". I mean, the line breaks do not show/work. ctxPaint.fillText("s ome \n \\n
thing", x, y); The above code will draw "s ome \n
thing", on one line. Is this a…
Spectraljump
  • 4,189
  • 10
  • 40
  • 55
134
votes
15 answers

How to break out of nested loops?

If I use a break statement, it will only break inner loop and I need to use some flag to break the outer loop. But if there are many nested loops, the code will not look good. Is there any other way to break all of the loops? (Please don't use goto…
user966379
  • 2,823
  • 3
  • 24
  • 30
128
votes
6 answers

Using continue in a switch statement

I want to jump from the middle of a switch statement, to the loop statement in the following code: while (something = get_something()) { switch (something) { case A: case B: break; default: // get another…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
124
votes
5 answers

How can I use break or continue within for loop in Twig template?

I try to use a simple loop, in my real code this loop is more complex, and I need to break this iteration like: {% for post in posts %} {% if post.id == 10 %} {# break #} {% endif %}

{{ post.heading }}

{% endfor %} How…
Victor Bocharsky
  • 11,930
  • 13
  • 58
  • 91
123
votes
3 answers

Breaking loop when "warnings()" appear in R

I am having an issue: I am running a loop to process multiple files. My matrices are enormous and therefore I often run out of memory if I am not careful. Is there a way to break out of a loop if any warnings are created? It just keeps…
mmann1123
  • 5,031
  • 7
  • 41
  • 49
122
votes
19 answers

How to kill a while loop with a keystroke?

I am reading serial data and writing to a csv file using a while loop. I want the user to be able to kill the while loop once they feel they have collected enough data. while True: #do a bunch of serial stuff #if the user presses the 'esc'…
Chris
  • 9,603
  • 15
  • 46
  • 67
109
votes
4 answers

How can I return something early from a block?

If I wanted to do something like this: collection.each do |i| return nil if i == 3 ..many lines of code here.. end How would I get that effect? I know I could just wrap everything inside the block in a big if statement, but I'd like to avoid…
ryeguy
  • 65,519
  • 58
  • 198
  • 260
109
votes
9 answers

break/exit script

I have a program that does some data analysis and is a few hundred lines long. Very early on in the program, I want to do some quality control and if there is not enough data, I want the program to terminate and return to the R console. Otherwise,…
user2588829
  • 1,523
  • 3
  • 10
  • 20
105
votes
4 answers

Why does C# have break if it's not optional?

When I create a switch statement in VS2008 C# like this (contrived): switch (state) { case '1': state = '2'; case '2': state = '1'; } it complains that I'm not allowed to drop through: Control cannot fall through from one…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
104
votes
16 answers

Why do we need break after case statements?

Why doesn't the compiler automatically put break statements after each code block in the switch? Is it for historical reasons? When would you want multiple code blocks to execute?
unj2
  • 52,135
  • 87
  • 247
  • 375
96
votes
10 answers

How do I exit a while loop in Java?

What is the best way to exit/terminate a while loop in Java? For example, my code is currently as follows: while(true){ if(obj == null){ // I need to exit here } }
BalaB
  • 3,687
  • 9
  • 36
  • 58
93
votes
7 answers

How to break ForEach Loop in TypeScript

I have a the below code, on which i am unable to break the loop on certain conditions. function isVoteTally(): boolean { let count = false; this.tab.committee.ratings.forEach((element) => { const _fo =…
Arka
  • 985
  • 1
  • 8
  • 15
85
votes
13 answers

Is it bad practice to use break to exit a loop in Java?

I was wondering if it is a "bad practice" to use a break statement to exit a loop instead of fulfilling the loop condition? I do not have enough insight in Java and the JVM to know how a loop is handled, so I was wondering if I was overlooking…
Don
  • 1,428
  • 3
  • 15
  • 31
83
votes
14 answers

Is there an equivalent to the "for ... else" Python loop in C++?

Python has an interesting for statement which lets you specify an else clause. In a construct like this one: for i in foo: if bar(i): break else: baz() the else clause is executed after the for, but only if the for terminates normally (not…
Delgan
  • 18,571
  • 11
  • 90
  • 141
80
votes
5 answers

Breaking out of a for loop in Java

In my code I have a for loop that iterates through a method of code until it meets the for condition. Is there anyway to break out of this for loop? So if we look at the code below, what if we want to break out of this for loop when we get to…
silverzx
  • 1,209
  • 4
  • 14
  • 18