Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Adding Labels to Method and Functions in Java

  • Last Updated : 29 Oct, 2020

The concept of labels in Java is being taken from assembly language. In Java break and continue are the control statements that control the flow of the program. Labels too can be considered as the control statement, but there is one mandatory condition, that within the loop, the label can only be used with break and continue keyword.

Usage of Labels:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

The break statement is helpful for coming out of the inner loop after the occurrence of some conditions, and the label is used to come out of the outer loop using the break statement in the inner loop.

The label is defined with a colon (:) after the name of the label, and before the loop.



Below is the demo syntax of the code using labels and without a label.

Without Using Label 

while (condition)  
{
 if (specific condition )
 { 
       break;
    // when control will reach to this break 
        // statement,the control will come out of while loop.
  } 
 else
     {
        // code that needs to be executed
    // if condition in if block is false.
     }
}

With Labels

// labelName is the name of the label
labelName:

while (condition)  
{
 if (specific condition )
     {  
       break labelName;
    // it will work same as if break is used here.
     }
 else
     {
        // code that needs to be executed 
    // if condition in if block is false.
     } 
}



Below are some programs using the main function which will help to understand how the label statements work, and where they can be used.

Use Of Label In Single For Loop

Java




// Java program to demonstrate 
// the use of label in for loop
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
    label1:
        for (int i = 0; i < 5; i++) {
            if (i == 3)
                break label1;
            System.out.print(i + " ");
        }
    }
}
Output
0 1 2 

Use of Label In Nested For Loop

Java




// Java program to demonstrate the use 
// of label in nested for loop
import java.io.*;
  
class GFG {
    
    public static void main(String[] args)
    {
    outerLoop:
        for (int i = 0; i < 5; i++) {
        innerLoop:
            for (int j = 0; j < 5; j++) {
                if (i != j) {
                    System.out.println("If block values "
                                       + i);
                    break outerLoop;
                }
                else {
                    System.out.println("Else block values "
                                       + i);
                    continue innerLoop;
                }
            }
        }
    }
}
Output
Else block values 0
If block values 0



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!