Software Development

Java – break outer loop from inner loop

[wp_ad_camp_1]

Whenever you use break; (or continue;), by default it only affects the current loop where it is invoked . If you are in a inner loop, surely the only loop that you can break; from is that loop. What if you need to break; from the outer-most loop in order to proceed to the next set of instructions?

We use Optional Labels.

No Labels

On this example, we are not using any labels and breaking from the inner loop.

[wp_ad_camp_2]

The codes display the following output.

With Labels

[wp_ad_camp_3]

On this example, we use a label named OUTER on the outer loop.

The codes display the following output.

[wp_ad_camp_4]

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like