Labeled "break" Statements

This section describes labeled 'break' statement, which is a branching statement that transfers the control to the end of the labeled enclosing block statement.

What Is Labeled "break" Statement? - A labeled "break" statement is a branching statement that transfers the control to the end of the labeled enclosing block statement.

Here is the syntax for a labeled "break" statement.

block_label : ... {
      break block_label
// break continue here

Labeled "break" statements extend usages of non-labeled "break" statements in 2 situations:

Here is a sample program that shows you how to use labeled "break" statements:

 * Copyright (c) All Rights Reserved.
class LabeledBreakStatementTest {
   public static void main(String[] arg) { out = System.out;

      out.println("\"break\" statement in a dummy block:");
      blockDummy : {
         out.println("   One");

         if (true) break blockDummy;
         out.println("   Two");
      out.println("   Three");

      out.println("\"break\" statement in an \"if\" block:");
      blockIf : if (true) {
         out.println("   Monday");

         if (true) break blockIf;
         out.println("   Tuesday");
      out.println("   Wednesday");

      out.println("\"break\" statement in a two-level block:");
      boolean isPrime;
      int i = 3;
      int operations = 1;

      blockOuter: while (true) { // block level 1
         isPrime = true;
         int j = 2;

         blockInner : while (j < i) { // block level 2
            isPrime = i%j > 0;

            if (!isPrime) break blockInner;

            if (operations > 100) break blockOuter;

         if (isPrime) out.println("   "+i+" is a prime number.");

If you compile and run the above program, you will see:

herong> java

"break" statement in a dummy block:

"break" statement in an "if" block:

"break" statement in a two-level block:
   3 is a prime number.
   5 is a prime number.
   7 is a prime number.
   11 is a prime number.
   13 is a prime number.
   17 is a prime number.
   19 is a prime number.
   23 is a prime number.

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

Control Flow Statements

 What Is Control Flow Statement

 "if-then" Statements

 "if-then-else" Statements

 Nested "if-then-else" Statements

 "switch" Statements

 Fall-Through Behavior of "switch" Statements

 Basic "for" Statements

 Enhanced "for" Statements

 "while" Statements

 "do" Statements

 "break" Statements

Labeled "break" Statements

 "continue" Statements

 Labeled "continue" Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials


 Full Version in PDF/EPUB