Adding Labels to Method and Functions in Java

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:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 + " ");
        }
    }
}

chevron_right


Output

0 1 2 

Use of Label In Nested For Loop

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
                }
            }
        }
    }
}

chevron_right


Output

Else block values 0
If block values 0

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.