Labeled Statements in Java

Java labeled blocks are logically similar to goto statements in C/C++.

A label is any valid Java identifier followed by a colon. For example, outer:, inner:, customLabel:, label_:.

1. Labeled Statement in String class

How many times, we have been told that “goto” statements are evil. I myself have read about this so-called evil concept through many respected authors of our time.

But, if we look at the source code of String.java, and check the code of public String toLowerCase(Locale locale) method, we will the use of labeled statement at line number 11.

public String toLowerCase(Locale locale) {
        if (locale == null) {
            throw new NullPointerException();
        }

        int firstUpper;
        final int len = value.length;

        /* Now check if there are any characters that need to be changed. */
        
        scan: {
            for (firstUpper = 0 ; firstUpper < len; ) {
                char c = value[firstUpper];
                if ((c >= Character.MIN_HIGH_SURROGATE)
                        && (c <= Character.MAX_HIGH_SURROGATE)) {
                    int supplChar = codePointAt(firstUpper);
                    if (supplChar != Character.toLowerCase(supplChar)) {
                        break scan;
                    }
                    firstUpper += Character.charCount(supplChar);
                } else {
                    if (c != Character.toLowerCase(c)) {
                        break scan;
                    }
                    firstUpper++;
                }
            }
            return this;
        }

        //More code
}

2. Labeled Statement with break and continue Keywords

In Java, we all know for what purpose the keywords break and continue exist. Basically, statements break and continue alter the normal control flow of the control flow statements or loops.

To use the labeled statement with break and continue, use the named label after it. For example:

break inner;

continue outer;

2.1. break keyword with labeled statement

hackit:
while (Some condition)
{
  if ( a specific condition )
  		break hackit;       //label
  else
  		//normal business logic goes here..
}

Whenever during the program execution, a labeled break statement is encountered then the control immediately goes out of enclosing labeled block.

2.2. continue keyword with labeled statement

Similarly, a labeled continue statement will bring control back to the start of the labeled block.

outer: for (int i = 0; i < 10; i++) {
  inner: for (int j = 10; j > 0; j--) {
    if (i != j) {
      System.out.println(i);
      break outer;
    }else{
      System.out.println("-->>" + i);
      continue inner;
    }
  }
}

Another example.

int a = 10;
int b = 12;

block1: {
    if (a < 0) {
      break block1;
    }
    if (b < 0) {
      break block1;
    }
    System.out.println( a + b );
  }
}

3. Conclusion

  • Java does not have a general goto statement.
  • The statements break and continue in Java alter the normal control flow of control flow statements. They can use labels which are valid java identifiers with a colon.
  • Labeled blocks can only be used with break and continue statements.
  • Labaled break and continue statements must be called within its scope. We can not refer them outside the scope of labeled block.
  • The break statement immediately jumps to the end (and out) of the appropriate compound statement.
  • The continue statement immediately jumps to the next iteration (if any) of the appropriate loop.
  • A continue statement does not apply to a switch statement or a block statement, only to compound statements e.g. for-loop, while-loop, and do-while loop.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “Labeled Statements in Java”

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.