Labelled Loop in Java
In Java, we can give a label to a loop. When we place a label before any loop, it is called labelled loop in Java. A label is a valid variable name (or identifier) in Java that represents the name of the loop to where the control of execution should jump.
The syntax for creating a labeled loop is straightforward. To label a loop, place the label (identifier) before the loop with a colon at the end. The general syntax to give a label to loops is as:
labelname:
for(initialization; test-condition; incr/decr) {
// code to be executed.
}
or,
labelname: for(initialization; test-condition; incr/decr) {
// code to be executed.
}
Example 1:
Loop1: for(initialization; test-condition; incr/decr) {
// code to be executed.
}
Example 2:
int i = 1;
Loop2: while(i <= 3) {
// code to be executed.
}We can also use the continue and break statements with a label like this:
Syntax:
continue labelName: // It is called labelled continue statement.
Similarly,
break labelName: // It is called labelled break statement.In the above syntax, labelName represents the name of loop. We mainly use the labelled break statement or labelled continue statement when we want to jump outside a nested loop or to continue a loop that is outside the current one.
Example 3:
Let us take a very simple example where we will find all even numbers in a two-dimensional array using labeled loops. The array contains three rows, having each row has four elements. We will use a labeled loop to traverse through the array and identify the even numbers.
package javaProgram;
public class LabeledLoopExample {
public static void main(String[] args)
{
// Creating a two-dimensional array of elements.
int[][] numbers = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
// Labeling the outer loop as 'outerLoop'
outerLoop: for(int i = 0; i < numbers.length; i++)
{
// Labeling the inner loop as 'innerLoop'
innerLoop: for(int j = 0; j < numbers[i].length; j++)
{
if(numbers[i][j] % 2 == 0) {
System.out.println(numbers[i][j]);
}
}
}
}
}
Output:
2
4
6
8
10
12
In this example, we have used two nested for loops. We have labeled the outer loop with a name “outerLoop” and the inner loop with a name “innerLoop”. The outer loop iterates over each row of the two-dimensional array, and the inner loop iterates over each element within that row.
Within the inner loop, we have checked the number is even or not. If the current number divides by 2 and the remainder is 0, then it means that the number of even. We have displayed the result on the console.
Java Labelled Loop Example Program
Let’s take an example program based on labelled loop in Java where we will use a for loop inside another for loop that displays i and j values. It means that we are using nested for loops.
In this program, we will use a label (name) to represent the outer for loop as outer and labelled continue statement. Look at the source code to understand better.
Example 4:
package javaProgram;
public class LabelledLoopEx {
public static void main(String[] args)
{
// Outer loop.
outer: for(int i = 1; i < 5; i++)
{
System.out.println(i);
// Inner loop.
for(int j = 1; j < 3; j++)
{
System.out.println(j);
if(i == j)
continue outer;
}
}
}
}Output:
1 1 2 1 2 3 1 2 4 1 2
Let’s understand how the flow of execution of this program is working:
a) When i = 1, it displays a value 1. Then, enter inside the inner for loop.
b) Since j is initially set to a value 1, it will print 1. Now, the continue statement will terminate inner loop when i == j and continues with the next iteration of outer loop (counting i).
c) In the next iteration of outer for loop, the value of i will be incremented by 1 and it will display 2 on the console. Then, the control of execution enters inside the inner for loop.
d) Since the value of i is 2, therefore, i is not equal j in the inner for loop iteration. Hence, when i = 2, the values of j will change from 1 to 2 and print 1, 2 on the console.
e) After the complete iteration of inner for loop, the control of execution again goes back to the outer for loop for the next iteration.
This process continues until the value of i is greater or equal to 5.
2. Let’s take another example program based on labelled loop where we will use break statement to come out of nested loops. Look at the source code and its explanation to understand it better.
Example 5:
package javaProgram;
public class LabelledLoopEx2 {
public static void main(String[] args)
{
// Outer loop.
outer: for(int i = 1; i < 3; i++)
{
System.out.println("i: " +i);
// Inner loop.
int j = 1;
while(j < 3)
{
System.out.println("j: " +j);
int x = i + j;
if(x > 2)
break outer;
j++;
}
}
System.out.println("Jumping out of both labelled loops");
}
}Output:
i: 1
j: 1
j: 2
Jumping out of both labelled loops
As you can observe in this program, the label outer labels the outer loop, and the labelled break statement terminates the flow of execution to come out of both loops.
Let’s take an example program where we will use both continue and break statements and produce the following output.
Example 6:
package javaProgram;
public class ContinueUse {
public static void main(String[] args)
{
// Outer loop.
outer: for(int i = 1; i < 100; i++)
{
System.out.println(" ");
if(i >= 10) break;
for(int j = 1; j < 100; j++)
{
System.out.printf(" * ");
if(j == i)
continue outer;
}
}
System.out.println("Termination by break statement");
}
}Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
Termination by break statement
Let’s take an example in which we will break out multiple nested loops simultaneously by using labeled loops. Dealing with complex data structures or algorithms becomes much easier with the help of this feature.
Example 7:
package javaProgram;
public class LabeledLoopExample {
public static void main(String[] args)
{
outerLoop: for (int i = 0; i < 3; i++)
{
innerLoop: for (int j = 0; j < 3; j++)
{
if (i == 1 && j == 1)
{
continue outerLoop; // This will skip the rest of the inner loop
}
System.out.println("i: " + i + ", j: " + j);
}
}
}
}
Output:
i: 0, j: 0
i: 0, j: 1
i: 0, j: 2
i: 1, j: 0
i: 2, j: 0
i: 2, j: 1
i: 2, j: 2
In this example, we have used two nested loops, an outer loop labeled as outerLoop, and an inner loop labeled as innerLoop. The outer for loop will iterate over each value of i from 0 to 2. Similarly, the inner for loop will also iterate over each value of j from 0 to 2.
Inside the inner loop, we have checked a conditional expression i == 1 and j == 1 using if statement. If this conditional expression is true, the continue outerLoop; statement will execute, which immediately jumps to the next iteration of the outer loop, skipping the rest of the inner loop for that particular value of i and j.
As you can observe, the execution of inner for loop is skipped for i == 1 and j == 1, resulting in only six iterations instead of nine. This example exhibits how labeled loops we can use with continue to control the flow of execution within nested loops effectively.
Advantages of Labelled Loop in Java
There are the following advantages of using labeled loops in Java program. They are:
- Labeled loops enhance the level of control over loop execution, making complex scenarios more manageable.
- Properly labeled loops can make the code more readable and self-explanatory.
- Labeled loops enable us to break out multiple nested loops simultaneously.
Best Practices for Using Labeled Loops
Labelled loop is a powerful provided by Java. Here, we have mentioned some key points that you should keep in mind for the best practices:
- Give descriptive and meaningful labels that describe the purpose of the loop.
- Avoid excessive nesting to keep the code clean and understandable.
- Always write clear and well-defined code within the labeled loops so that it should be easy to understand.
- Ensure that the label used with the break or continue statement corresponds to the correct loop.
- Be careful to avoid accidentally creating infinite loops.
Labeled Loops vs Unlabeled Loops
In comparison to unlabeled loops, labeled loops add an extra layer of control and can handle more complex situations. However, in simple scenarios, using labeled loops might introduce unnecessary complexity.





