Labelled Loop in Java | Example Program

Labelled Loop in Java | In Java, we can give a label to a loop. A label is a valid variable name in Java that represents the name of the loop to where the control of execution should jump.

To label a loop, place the label before the loop with a colon at the end. The general syntax to give a label to loops is as follows:

labelname:
for(initialization; test-condition; incr/decr){
 // code to be executed.
}
or,
labelname: for(initialization; test-condition; incr/decr){
 // code to be executed.
}
For example 1:
 Loop1: for(initialization; test-condition; incr/decr){
 // code to be executed.
}
For example 2:
int i = 1;
Loop2: while(i <= 3){
 // code to be executed.
}

The continue and break statements can also be used with a label like this:

Syntax:
       continue labelname: // It is called labelled continue statement.
// Here, labelname represents the name of loop.
Similarly,
       break labelname: // It is called labelled break statement.

The labelled break statement or labelled continue statement is mainly used when we want to jump outside a nested loop or to continue a loop that is outside the current one.

Example Program based on Labelled Loop in Java


1. 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.

Program source code 1:

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.

Program source code 2:

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.

3. Let’s take an example program where we will use both continue and break statements and produce the following output.

Program source code 3:

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

Hope that this tutorial has covered all the important points concerning with labelled loop in Java with example programs. I hope that you will have understood how and when to use labelled loops in Java program.
Thanks for reading!!!
Next ⇒ Packages in Java

⇐ Prev Next ⇒