How to break out of nested loops?
Use:
if (condition) {
i = j = 1000;
break;
}
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
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 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.
Breaking out of nested loops
It has at least been suggested, but also rejected. I don't think there is another way, short of repeating the test or re-organizing the code. It is sometimes a bit annoying.
In the rejection message, Mr van Rossum mentions using return
, which is really sensible and something I need to remember personally. :)
When breaking out of a nested loop, how do I make it so that the inner loop isn't ignored in Python?
The way you have it written right now, the inner loop will always break on the first iteration (when i = 0
). This is why you are only seeing it print once, the outer loop is looping 5 times, however the inner loop only ever gets through the first iteration before hitting break
.
See below, the break
line should be nested inside the if statement so it only breaks from the inner loop when the two numbers match.
for i in range(5):
for j in range(5):
if i == j:
print('Same Number')
break
Related Topics
Saving Any File to in the Database, Just Convert It to a Byte Array
Easiest Way to Parse JSON Response
Error: "Cannot Modify the Return Value" C#
How to Configure Socket Connect Timeout
Interop Type Cannot Be Embedded
Sqlcommand Parameters Add VS. Addwithvalue
How to Set an SQL Server Connection String
Recommended Servicestack API Structure
Run Single Instance of an Application Using Mutex
How to Convert Seconds into (Hour:Minutes:Seconds:Milliseconds) Time
Using String Format to Show Decimal Up to 2 Places or Simple Integer
How to Read a File Even When Getting an "In Use by Another Process" Exception
Intersection of Multiple Lists with Ienumerable.Intersect()
What Does Initializecomponent() Do, and How Does It Work in Wpf
Parallel.Foreach VS Task.Factory.Startnew
How to Change Menu Hover Color