Label in Java

label in java

Label in Java

The label statement in Java is used to prefix a statement with an identifier that can be referred and is used with break and continue statements. It can be specified by any name other than the reserved words in Java. Syntax of label statement is:

Syntax:

label:
  statements;

 

 

Label in Java with Continue Statement

A label is used to put a name to a loop. When used with the continue statement, it is usually done if you want to go to the next iteration of the outer loop and not of the inner loop which contains the continue statement and the outer and inner loops are labeled.

Syntax:

continue labelName;

Example of Label in nested loops with Continue Statement in Java

//Java Program with labels in nested loop using continue statement
public class LabelContinue {
    public static void main(String[] args) {

        //Number of symbols in each row is equal to its row number
        //prints "@" for every but third
        loop1: for(int i=1;i<=5;i++){ //rows
            loop2: for(int j=1;j<=i;j++){ //columns
                if(i==3){
                    continue loop1;
                }
                System.out.print("@");
            }
            System.out.println();
        }
    }
}

In the program provided above, nested loop is used. Outer loop is labelled loop1 and inner loop is labelled loop2. The code prints a pyramid pattern of “@” symbol for every except for third row. Since rows are controlled by outer loop and columns are controlled by inner loop, continue with outer loop label is used when condition is met which is “i==3” where “i” is a loop variable for outer loop that controls row.

Output:

@
@@   
@@@@ 
@@@@@

 

 

Label in Java with Break Statement

A label is used to put a name to a loop. When used with a break statement, it is usually done if you want to come out of the outer loop and not of the inner loop which contains the break statement and the outer and inner loops are labeled.

Syntax:

break labelName;

Example of Label in nested loops with Break Statement in Java

//Java Program with Label in nested loop using break statement
public class LabelBreak {
    public static void main(String[] args) {
        
        //Breaks out of the loop when #row=3 and #column=2
        loop1: for(int i=1;i<=5;i++){ //rows
            loop2: for(int j=1;j<=i;j++){ //columns
                if(i==3 &&j==2){
                    break loop1;
                }
                System.out.print("@");
            }
            System.out.println();
        }
    }
}

In the program provided above, nested loop is used. Outer loop is labelled loop1 and inner loop is labelled loop2. The code prints a pyramid pattern of “@” symbol where every row has number of characters equal to the row number. Since rows are controlled by outer loop and columns are controlled by inner loop, break with outer loop label is used when condition is met which is “i==3 && j==2” where “i” is a loop variable for outer loop that controls row and “j” is a loop variable for inner loop that controls column.

Output:

@
@@
@

 

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *