Java break Statement

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.

In simple words, the break statement has two uses:

  • it terminates the current loop of any type (forwhiledo-while);
  • it terminates a case in the switch statement;

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. Break Statement Example

The following example demonstrates a loop that includes one break. In this example, the condition to continue the loop is always true, but it will be successfully stopped when the variable i becomes 0 through the use of break inside the conditional statement.

int i = 10;
while (true) { // the condition to continue the loop
    if (i == 0) { // the condition to perform the break that stops this loop 
        break;
    }
    i--;
}

The break statement terminates only the loop in which it is currently located. If this loop is performed inside another loop, the outer loop won’t be stopped.

for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        System.out.print(j + " ");
        if (i == j) {
            break;
        }
    }
    System.out.println();
}

In this example, the break statement can’t stop the outer loop (with variable i) and the code prints:

0 
0 1 
0 1 2 
0 1 2 3 
0 1 2 3 4 
0 1 2 3 4 5 
0 1 2 3 4 5 6 
0 1 2 3 4 5 6 7 
0 1 2 3 4 5 6 7 8 
0 1 2 3 4 5 6 7 8 9 

To stop the outer loop we could declare a Boolean variable stopped and use it as a special Boolean flag.

boolean stopped = false;

for (int i = 0; i < 10 && !stopped; i++) {
    for (int j = 0; j < 10; j++) {
        System.out.print(j + " ");
        if (i == j) {
            stopped = true;
            break;
        }
     }
    System.out.println();
}

Now, the program’s output is not the same:

0

3. 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

3.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;
}

3.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;
}

4. The break Statement with ‘while‘ Loop

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

5. Conclusion

The break statement allows for the termination of loops or switch cases based on certain conditions. It can be a valuable tool for controlling the flow of your program, especially when you need to exit loops prematurely.

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

Happy Learning !!

Comments

Subscribe
Notify of
guest
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

Dark Mode

Dark Mode