Labeled Statements
Breaking from nested loops
Interview Relevant: Advanced loop control concept
4 min read
Labeled Statements in Java
Labels allow you to name a statement, typically a loop, so you can use break or continue to control outer loops from within nested loops.
Syntax
labelName: statement // With break break labelName; // With continue continue labelName;
ā Use Case: Breaking out of multiple nested loops when a condition is met, without using boolean flags.
Label Naming Conventions
- Use descriptive names (OUTER, SEARCH, ROW_LOOP)
- Uppercase is common convention
- Can be any valid identifier
ā ļø Best Practice: Labels are rarely needed. Consider extracting nested loops into a separate method with return instead.
Code Examples
Labeled break exits the labeled outer loop
java
1// Labeled break - exit outer loop
2OUTER: for (int i = 1; i <= 3; i++) {
3 for (int j = 1; j <= 3; j++) {
4 System.out.println("i=" + i + ", j=" + j);
5 if (i == 2 && j == 2) {
6 break OUTER; // Exits BOTH loops
7 }
8 }
9}
10System.out.println("Done!");
11/*
12i=1, j=1
13i=1, j=2
14i=1, j=3
15i=2, j=1
16i=2, j=2
17Done!
18*/Labeled continue skips to next outer loop iteration
java
1// Labeled continue - skip to next outer iteration
2OUTER: for (int i = 1; i <= 3; i++) {
3 for (int j = 1; j <= 3; j++) {
4 if (j == 2) {
5 continue OUTER; // Skip to next i
6 }
7 System.out.println("i=" + i + ", j=" + j);
8 }
9}
10/*
11i=1, j=1
12i=2, j=1
13i=3, j=1
14*/Search pattern with and without labels
java
1// Practical: Search in 2D array
2int[][] matrix = {
3 {1, 2, 3},
4 {4, 5, 6},
5 {7, 8, 9}
6};
7int target = 5;
8
9SEARCH: for (int row = 0; row < matrix.length; row++) {
10 for (int col = 0; col < matrix[row].length; col++) {
11 if (matrix[row][col] == target) {
12 System.out.println("Found at [" + row + "][" + col + "]");
13 break SEARCH; // Exit completely when found
14 }
15 }
16}
17
18// Without label, need a flag variable
19boolean found = false;
20for (int row = 0; row < matrix.length && !found; row++) {
21 for (int col = 0; col < matrix[row].length && !found; col++) {
22 if (matrix[row][col] == target) {
23 found = true;
24 }
25 }
26}Cleaner alternative using method extraction
java
1// Alternative: Extract to method (preferred)
2public static int[] findElement(int[][] matrix, int target) {
3 for (int i = 0; i < matrix.length; i++) {
4 for (int j = 0; j < matrix[i].length; j++) {
5 if (matrix[i][j] == target) {
6 return new int[]{i, j}; // Return exits all loops!
7 }
8 }
9 }
10 return null; // Not found
11}
12
13// Usage
14int[][] matrix = {{1,2,3}, {4,5,6}, {7,8,9}};
15int[] pos = findElement(matrix, 5);
16if (pos != null) {
17 System.out.println("Found at [" + pos[0] + "][" + pos[1] + "]");
18}Use Cases
- Breaking out of deeply nested loops
- Search algorithms in multi-dimensional data
- Complex nested iteration with early exit
- Matrix operations requiring early termination
- Parsing nested structures
Common Mistakes to Avoid
- Overusing labels when method extraction is cleaner
- Misspelling label name
- Using label with non-loop statement incorrectly
- Making code harder to read with too many labels
- Not placing label directly before loop statement
- Using goto-style programming (labels aren't goto!)