How to Use Break to Exit Multiple Nested 'For' Loops

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

AFAIK, C++ doesn't support naming loops, like Java and other languages do. You can use a goto, or create a flag value that you use. At the end of each loop check the flag value. If it is set to true, then you can break out of that iteration.

How to break out of nested loops?

Use:

if (condition) {
i = j = 1000;
break;
}

How can I break out of multiple loops?

My first instinct would be to refactor the nested loop into a function and use return to break out.

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

Just like Perl,

loop1:
for (var i in set1) {
loop2:
for (var j in set2) {
loop3:
for (var k in set3) {
break loop2; // breaks out of loop3 and loop2
}
}
}

as defined in EMCA-262 section 12.12. [MDN Docs]

Unlike C, these labels can only be used for continue and break, as Javascript does not have goto.

Breaking out of a nested loop

Well, goto, but that is ugly, and not always possible. You can also place the loops into a method (or an anon-method) and use return to exit back to the main code.

    // goto
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
goto Foo; // yeuck!
}
}
Foo:
Console.WriteLine("Hi");

vs:

// anon-method
Action work = delegate
{
for (int x = 0; x < 100; x++)
{
for (int y = 0; y < 100; y++)
{
return; // exits anon-method
}
}
};
work(); // execute anon-method
Console.WriteLine("Hi");

Note that in C# 7 we should get "local functions", which (syntax tbd etc) means it should work something like:

// local function (declared **inside** another method)
void Work()
{
for (int x = 0; x < 100; x++)
{
for (int y = 0; y < 100; y++)
{
return; // exits local function
}
}
};
Work(); // execute local function
Console.WriteLine("Hi");

How do I break out of nested loops in Java?

Like other answerers, I'd definitely prefer to put the loops in a different method, at which point you can just return to stop iterating completely. This answer just shows how the requirements in the question can be met.

You can use break with a label for the outer loop. For example:

public class Test {
public static void main(String[] args) {
outerloop:
for (int i=0; i < 5; i++) {
for (int j=0; j < 5; j++) {
if (i * j > 6) {
System.out.println("Breaking");
break outerloop;
}
System.out.println(i + " " + j);
}
}
System.out.println("Done");
}
}

This prints:

0 0
0 1
0 2
0 3
0 4
1 0
1 1
1 2
1 3
1 4
2 0
2 1
2 2
2 3
Breaking
Done

How to break out of nested loops in Dart

Using a label

Dart supports labels. Create a label name and follow it with a colon:

outerLoop:
for (int i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
print('i: $i, j: $j');
break outerLoop;
}
}

In the example above, the label name is outerLoop, but you could have named it something else. Calling break outerLoop breaks out of the for loop that outerLoop is in front of.

This prints:

i: 0, j: 0

Returning from a function

Another option is to put the nested loops inside a function and then just return from the function:

void myFunction() {
for (int i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
print('i: $i, j: $j');
return;
}
}
}


Related Topics



Leave a reply



Submit