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
33
votes
1 answer

Using break statement in switch

Following is the given example for using break statements in switch: let numberSymbol: Character = "三" // Simplified Chinese for the number 3 var possibleIntegerValue: Int? switch numberSymbol { case "1", "١", "一", "๑": possibleIntegerValue =…
avi
  • 9,292
  • 11
  • 47
  • 84
32
votes
14 answers

Breaking out of a loop from within a function called in that loop

I'm currently trying to figure out a way to break out of a for loop from within a function called in that loop. I'm aware of the possibility to just have the function return a value and then check against a particular value and then break, but I'd…
Magisch
  • 7,312
  • 9
  • 36
  • 52
32
votes
11 answers

Break statement inside two while loops

Let's say I have this: while (a) { while (b) { if (b == 10) { break; } } } Question: Will the break statement take me out of both loops or only from the inner one? Thank you.
adrian
  • 4,574
  • 17
  • 68
  • 119
31
votes
3 answers

PHP - Break after return?

do I need to use break here or will it stop looping and just return once? for($i = 0; $i < 5; $i ++) { if($var[$i] === '') return false; // break; } Thank you!
headacheCoder
  • 4,503
  • 8
  • 30
  • 33
31
votes
2 answers

Exit iteration of for loop Swift iOS

I have a function with a for loop inside of it: func example() { // create tasks for link in links { let currIndex = links.indexOf(link) if let im = story_cache?.objectForKey(link) as? UIImage { if ((currIndex != nil) &&…
Alk
  • 5,215
  • 8
  • 47
  • 116
30
votes
1 answer

What is the meaning of "break 2"?

I always used and seen examples with just "break". What is the meaning of this:
funguy
  • 2,072
  • 7
  • 30
  • 41
29
votes
9 answers

Control Break out of Infinite Loop In 2010 (2013) Excel VBA

If I write code creating an infinite loop, with my new Excel, the Ctrl + Break no longer works. Neither does the Esc key, etc. I've looked all over the web and it appears that Microsoft has a bug and doesn't care to fix it. Is there a way to…
Tommy Z
  • 649
  • 5
  • 12
  • 18
29
votes
7 answers

Perl Breaking out of an If statement

This one just came up: How do I break out of an if statement? I have a long if statement, but there is one situation where I can break out of it early on. In a loop I can do this: while (something ) { last if $some_condition; blah, blah,…
David W.
  • 105,218
  • 39
  • 216
  • 337
29
votes
7 answers

F# break from while loop

There is any way to do it like C/C#? For example (C# style) for (int i = 0; i < 100; i++) { if (i == 66) break; }
cheziHoyzer
  • 4,803
  • 12
  • 54
  • 81
28
votes
9 answers

Jumping from one case to the default case in switch statement

switch(ch){ case 'a': //do something, condition does not match so go to default case //don't break in here, and don't allow fall through to other cases. case 'b': //.. …
thetux4
  • 1,583
  • 10
  • 25
  • 38
28
votes
9 answers

How does continue work?

I am trying to understand how "continue" works. I understood the concept of the keyword but when I run different programs, it works differently :-/ Let me show you few examples: If I run this program : int j = 0; int i = 0; LABEL1: for (; i < 3;…
user3884432
  • 291
  • 3
  • 6
27
votes
4 answers

Naming Loops in Python

I recently read this question which had a solution about labeling loops in Java. I am wondering if such a loop-naming system exists in Python. I have been in a situation multiple times where I do need to break out of an outer for loop from an inner…
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
26
votes
4 answers

Wrapping long email addresses in small boxes

I have a box with a width of 118px which contains an email address. I use word-wrap: break-word; to wrap the addresses better. But on a special kind of addresses this makes it worse. big.ass.email@addre ss- is.extremely.lame.de Because of…
Juuro
  • 1,487
  • 5
  • 16
  • 27
25
votes
9 answers

Get out of multiple loops?

Possible Duplicate: Breaking out of a nested loop I have this code foreach (___) { foreach (___) { foreach (___) { if (condition) { //break out of all loops } } …
user1153455
25
votes
8 answers

How to break out of a function

If I have a function as follows: void func () { //... if (condition) { break; } } When I use break it gives me an error. Is there another way to exit a function using an if condition and to complete compiling the code normally?
Shadi
  • 441
  • 3
  • 8
  • 13