Labelled breaks breaks out of several levels of nested loops inside a pair of curly braces.
public class Main { public static void main(String args[]) { int len = 100; int key = 50; int k = 0; out: { for (int i = 0; i < len; i++) { for (int j = 0; j < len; j++) { if (i == key) { break out; } k += 1; } } } System.out.println(k); } }
1. | Break Demo | ||
2. | Break With Label Demo | ||
3. | Continue Demo | ||
4. | Continue With Label Demo | ||
5. | Demonstrates break and continue keywords |