Java break Keyword

The break keyword in Java is used to terminate for, while, or do-while loops. It may also be used to terminate a switch statement as well.

1. Syntax

The syntax is pretty much simple. Use the break keyword with a semicolon (;). We can additionally use a label as well.

while (testExpression) {
  //statement(s)
  
  if(break-condition)
    break;

  //statement(s)
}

//statement(s)

In the above example, the loop is terminated immediately when the break statement is encountered. The flow control is then moved to the next statement after the loop.

2. Type of break Statements

A break statement is used to exit from a block. There are two forms of break Statements:

  • Unlabeled break statement
  • Labeled break statement

2.1. Unlabeled break Statement

Unlabeled break statements are without any labels. They are written as simply "break;". An example of an unlabeled break statement is:

int i = 1;

while (true) {   // Cannot exit the loop from here

  if (i <= 10) {

    System.out.println(i);
    i++;
  } else {

    break; // Exit the loop
  }
}

OR, we know how to use them in switch statements.

switch (switch-expression) {

  case label1:
    statements;
    break;

  case label2:
    statements;
    break;

  default:
    statements;
}

2.2. Labeled break Statement

Here, we write a label name after break keyword. An example of a labeled break statement is :

break label_name;

A more detailed example could be:

blockLabel:
{
  int i = 10;
  if (i == 5) {
    break blockLabel;   // Exits the block
  }

  if (i == 10) {
    System.out.println("i is not five");
  }
}

The break statement terminates the labeled statement; it does not transfer the control flow to the label. Control flow is transferred to the statement immediately following the labeled (terminated) statement.

One important point to remember about a labeled break statement is that the label used with the break statement must be the label for the block in which that labeled break statement is used.

The following snippet of code illustrates an incorrect use of a labeled break statements:

lab1:
{
  int i = 10;
  if (i == 10)
    break lab1; // Ok. lab1 can be used here
}

lab2:
{
  int i = 10;
  if (i == 10)    // A compile-time error. lab1 cannot be used here because this block is not
                  // associated with lab1 label. We can use only lab2 in this block
    break lab1;
}

3. break Keyword Example

The following is a Java program to print the numbers from 1 to 5 and then break the while loop.

int i = 1;

while (true)
{
  if(i > 5)
    break;

  System.out.println(i);
  i++;
}

Program output.

1
2
3
4
5

That’s all for the Java break keyword and its usage.

Happy Learning !!

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial