Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is it possible to use the break function to exit several nested for loops?

If so, how would you go about doing this? Can you also control how many loops the break exits?

like image 314
Faken Avatar asked Aug 10 '09 23:08

Faken


People also ask

Does Break exit multiple loops?

In a nested loop, a break statement only stops the loop it is placed in. Therefore, if a break is placed in the inner loop, the outer loop still continues. However, if the break is placed in the outer loop, all of the looping stops.

Is a single break statement is sufficient to terminate the nested loop?

No, don't spoil it with a break .

How can you break out of a for loop inside a nested loop?

There are two steps to break from a nested loop, the first part is labeling loop and the second part is using labeled break. You must put your label before the loop and you need a colon after the label as well. When you use that label after the break, control will jump outside of the labeled loop.


1 Answers

No, don't spoil it with a break. This is the last remaining stronghold for the use of goto.

like image 143
Henk Holterman Avatar answered Sep 29 '22 00:09

Henk Holterman